ruby中引入模块module的概念,在定义类的时候能够经过对模块进行组装来生成需求的类,如此能够在继承的基础上加强代码的复用行. ruby
若是类所include的模块中包含相同的方法时会出现什么状况呢? 测试
module Robot1 def say puts "1" end end module Robot3 def say puts "3" end end module Robot2 def say puts "2" end end class Robot include Robot1 include Robot2 include Robot3 end robot=Robot.new robot.say输出结果: 3
结果分析: 最后面include的模块会覆盖以前模块的相同方法. spa
class Robot include Robot1 include Robot3 include Robot2 end输出结果: 2
结果分析:支持一开始的猜测. code
class Robot include Robot1 include Robot2 include Robot3 include Robot2 end输出结果: 3
结果分析: 重复引用将不会编译的时候不会在执行. 继承
自学阶段,若有不对,敬请指出. 编译