我了解常规的方法查找路径,即类、超类/模块,一直到BasicObject
。我认为这对于链的单件版本也是正确的,但当您在元链中混入一个模块时,似乎并非如此。在下面的例子中,当我将Automobile
模块包含在Vehicle的特有类中时,调用了Automobile
模块的banner
方法而不是其单件版本,如果有人能解释一下这是为什么,我会很感激。
module Automobile
def banner
"I am a regular method of Automobile"
end
class << self
def banner
"I am a class method of Automobile"
end
end
end
class Vehicle
def banner
"I am an instance method of Vehicle"
end
class << self
include Automobile
def banner
puts "I am a class method of Vehicle"
super
end
end
end
class Car < Vehicle
def banner
"I am an instance method of Car"
end
class << self
def banner
puts "I am a class method of Car"
super
end
end
end
puts Car.banner
# I am a class method of Car
# I am a class method of Vehicle
# I am a regular method of Automobile
include
类方法。 - Patrick OscityVehicle.banner
和Vehicle.new.banner
)。因此,阻止Automobile单例banner
方法被Vehicle继承似乎对我来说是不一致的。 - saihgalainclude
仅包括实例方法。类继承两者都包括。请记住,模块只是将其他代码分组的机制。 - Patrick Oscity类(Class)
是一个模块(Module)
,并且类(Class)
有比模块(Module)
更多的功能。 - William Entriken