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的两种用法
 a) 体现namespace概念;
 b) 被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

© 2005, bigwhite. 版权所有.

Related posts:

  1. 结识Ruby
  2. Java 5.0新特性研究(一)
  3. Java 5.0新特性研究(二)
  4. Effective Java阅读笔记-item16
  5. Oolong and Gnoloo