class Foo
def initialize(a)
puts "Hello #{a}"
end
end
module Bar
def initialize(b)
puts "#{b} World"
end
end
class Sample < Foo
include Bar
def initialize(c)
super
end
end
Sample.new('qux') #=> qux World
Run Code Online (Sandbox Code Playgroud)
为什么输出不是'Hello qux'?代码的功劳
Jon*_*her 10
当您将一个模块包含到一个类中时,它就像您在类层次结构中插入一个新的超类一样,就在Sample和Foo之间.调用super()通过包含的模块进行搜索,然后再回到真正的超类(Foo).