新手问题 请问 Ruby 中类方法和单件类 (singleton class) 方法的区别

whitecrow · 2013年01月16日 · 最后由 msl12 回复于 2016年10月11日 · 3330 次阅读

看 activesupport 源码时看到:

module Kernel
  def singleton_class
    class << self
      self
    end 
  end unless respond_to?(:singleton_class) 
end

不知道class << self定义的单件类方法,和def self.method的类方法有什么区别呢?

没有区别,都是 singleton method

#1 楼 @tumayun 非常感谢 :D 终于弄清楚这个概念了。

类方法的实质就是一个类的单件方法

需要 登录 后方可回复, 如果你还没有账号请 注册新账号