mixin

Matz 的一篇 PPT “Object-Oriented scripting in Ruby”中, Matz 提到 Ruby 提供一种语言机制 Mix-in ,在其 PPT 中如是描述的“ No Multiple Inheritance but Mix-in ”、“ Mix-in is as strong as multiple inheritance but simple ”。

 

“module” in Ruby

C++ Java C# 中都有 namespace 的概念,是用来隔离代码,防止代码冲突的。在 Ruby 中是采用“ module ”这个关键字来完成 namespace 功能的。

 

//considering the following code:

//MicrosoftCompiler.rb

module Microsoft

         def Microsoft.compile

                   print "This is microsoft's compiler", "/n"

         end

end

 

//SunCompiler.rb

module Sun

         def Sun.compile

                   print "This is Sun's compiler", "/n"

         end

end

                                                                          

//TestCompiler.rb

require "D://Ruby//MicrosoftCompiler.rb"

require "D://Ruby//SunCompiler.rb"

 

Microsoft.compile

Sun.compile

 

//output:

>ruby TestComplier.rb

This is microsoft's compiler

This is Sun's compiler

>Exit code: 0

 

module 除了体现 namespace 的概念外,从上面代码中我们还可以看到在两个 module 中定义的 method 和调用这个 method 时都在其前面加上其所在 module 的名字了。这样定义的函数叫 module method 。但是不是所有要使用 module 中函数都要显式的加上 module 名字呢?答案:不是。请往下看。

 

Mix-in Another wonderful use of “module”

module method 的定义和调用方式让我们联想到 class method (注意不是 class instance method ),也让我们从 module 联想到 class 了。但是 A module 是不能像 class 那样拥有实例的, module 毕竟还不是 class ,但是我们可以在 class 的定义中“ include ”一个 module ,这样会发生什么呢?

 

//considering the following code:

module Mammal  # 哺乳动物

         def suckle   # 哺乳

                   print "I can suckle my baby" ,"/n"

         end

end

 

module Flyable   # 可飞行的

         def fly        # 飞行         

                   print "I can fly", "/n"

         end

end

 

class Chiropter    # 蝙蝠

         include Mammal          # 蝙蝠是哺乳动物

         include Flying               # 蝙蝠可以飞行

end

 

aChiropter = Chiropter.new

aChiropter.suckle

aChiropter.fly

 

//output

>ruby TestMixin.rb

I can suckle my baby

I can fly

>Exit code: 0

 

如上面代码得出的结果我们可以看出,一旦一个 class include 了一个 module, 那么所有 module 中的 methods 就好像成为 class’s instance methods 一样,这就是 mix-in ,看起来被 mix-in modules 的行为更像是这个 class super class ,通过这种方法我们可以间接实现多重继承。但是注意看看被 mixin module 中的函数是如何定义的?和前面代表 namespace 概念的 module 中的 method 定义不同的是在于定义时 method name 前不见了 module name ,这样定义出来的 method module instance method , 虽然 module 不能实例化,但是一旦被 mixin 到某个 class 中,这些 module instance method 的使用就等价于 class instance method 了。

 

总结:

1.         Module 的两种用法

Ø         体现 namespace 概念;

Ø         mixin class 中间接的实现类的多重继承。

2.         Module 中的两种 method 定义和用法

module module_name

       def module_name. module_method_name

              # module method , for representing namespace concept

       end

 

       def module_instance_method_name

              # module instance method

              # Once the module has been mixed in,

#the method will be equal to a class instance method

       end

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值