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

whitecrow · January 16, 2013 · Last by msl12 replied at October 11, 2016 · 3331 hits

看 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 终于弄清楚这个概念了。

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

You need to Sign in before reply, if you don't have an account, please Sign up first.