在module中使用extend

extend将module中实例方法添加到对象上变为对象的singleton_method方法单件方法。
在Ruby中一切都是对象,于是进行了一下测试
说明module也可以通过extend将一个模块中的实例方法变为自己的单件方法。因为module不能继承另一个module,所以这个单件方法不能像类一样可以传给子类

module M1
    def name
        puts 'M1'
    end
end

module M3
end

M3.extend(M1)
M3.name
module M4
     extend M1
end
M4.name
class C1
end
c1 = C1.new
c1.extend(M1)
p c1.singleton_methods;

你可能感兴趣的:(在module中使用extend)