1.子类和父类之间,子类继承父类的实例方法和类方法

class Father
  def self.class_method_demo
    puts "class method demo"
  end

  def instance_method_demo
    puts "instance method demo"
  end
end

class Son
end

Son.class_method_demo #"class method demo"
Son.new.instance_method_demo #"instance method demo"

2.不能include模块中的模块方法,只能include模块中的实例方法

module Father
  def self.class_method_demo
    puts "class method demo"
  end

  def instance_method_demo
    puts "instance method demo"
  end  
end

class Son
  include Father
end

Son.class_method_demo #error
Son.new.instance_method_demo #"instance method demo"

3.extend能引入模块中的实例方法为类方法,但是不能引入模块方法为类方法或者实例方法

module Father
  def self.class_method_demo
    puts "class method demo"
  end

  def instance_method_demo
    puts "instance method demo"
  end  
end

class Son
  extend Father
end

Son.class_method_demo #error
Son.instance_method_demo #"instance method demo"

results matching ""

    No results matching ""