模块
Modules
模块
Modules in ruby are similar to classes, except:
Ruby中的模块同类非常相似,但有些许不同:
- A module can have no instances.
- 模块不能有实例。
- A module can have no subclasses.
- 模块不能有子类。
- A module is defined by module … end.
- 模块是通过
module … end
定义的。
Actually… the Module class of module is the superclass of the Class class of class. Got that? No? Let’s move on.
实际上,module的Module
类是class的Class
类的父类。明白了吗?没有?让我们继续。
There are two typical uses of modules. One is to collect related methods and constants in a central location. The Math module in ruby’s standard library plays such a role:
模块有两种典型的用法。其一是在一个集中的位置收集相关的方法和常量。Ruby标准库中的数学模块发挥了这样的作用:
ruby> Math.sqrt(2)
1.41421
ruby> Math::PI
3.14159
The :: operator tells the ruby interpreter which module it should consult for the value of a constant (conceivably, some module besides Math might mean something else by PI).
这个::
操作符告诉Ruby解释器,它应该咨询哪个模块来得到一个常量的值(可以想象,除了Math之外的某个模块还可能用PI表达其他内容)。
If we want to refer to the methods or constants of a module directly without using ::, we can include that module:
如果我们想不使用::
而是直接引用某个模块的方法或常量,那么我们可以include
那个模块:
ruby> include Math
Object
ruby> sqrt(2)
1.41421
ruby> PI
3.14159
Another use of modules is called mixin. Some OO programming languages, including C++, allow multiple inheritance, that is, inheritance from more than one superclass.
另外一种使用模块的方法被称为mixin。许多OO编程语言,包括C++,允许多重继承,也就是说,从多个父类继承。
A real-world example of multiple inheritance is an alarm clock; you can think of alarm clocks as belonging to the class of clocks and also the class of things with buzzers.
多重继承的一个现实生活中的例子是闹钟。你可以把闹钟看作属于钟类,也可以看作属于蜂鸣器类。
Ruby purposely does not implement true multiple inheritance, but the mixin technique is a good alternative.
Ruby故意不实现真正的多重继承,但是mixin技术是一个不错的替代品。
Remember that modules cannot be instantiated or subclassed; but if we include a module in a class definition, its methods are effectively appended, or “mixed in”, to the class.
记住,模块不能被实例化或子类化,但是如果我们在一个类定义中包含了模块,它的方法便被有效地附加或“混合”到了类中。
Mixin can be thought of as a way of asking for whatever particular properties we want to have.
Mixin可以被认为是一种请求任何我们想要的特定属性的方式。
For example, if a class has a working each method, mixing in the standard library’s Enumerable module gives us sort and find methods for free.
比如,如果一个类有一个可工作的each
方法,那么混合标准库中的Enumerable
模块可以免费为我们提供sort
和find
方法。
This use of modules gives us the basic functionality of multiple inheritance but allows us to represent class relationships with a simple tree structure, and so simplifies the language implementation considerably (a similar choice was made by the designers of Java).
模块的使用提供了多重继承的基本功能,但允许我们用简单的树结构来表示类关系,这样就大大简化了语言的实现(Java设计者也做出了类似的选择)。