每次在类中使用include包含一个模块,都会影响那个类的实例必须处理对应方法名的消息过程,对于prepend来说也是一样,如果前置(prepand)一个模块到一个类中,对象首先在模块中查找,然后在类中查找。
module Mytalk
def say
puts "hello module"
end
end
class Teacher
prepend Mytalk
def say
puts "hello class"
end
end
teacher=Teacher.new
teacher.say
输出结果为 hello module
ruby-prepend
最新推荐文章于 2022-05-13 15:55:11 发布