Ruby 中的多重继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10254689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Inheritance in Ruby?
提问by Bhubhu Hbuhdbus
I thought that Ruby only allowed single inheritance besides mixin. However, when I have class Squarethat inherits class Thing, Thingin turn inherits Objectby default.
我认为Ruby除了mixin之外只允许单继承。但是,当我有Square继承 class 的 class 时Thing,默认情况下Thing又会继承Object。
class Thing
end
class Square < Thing
end
Doesn't this represent multiple inheritance?
这不代表多重继承吗?
回答by sawa
I think you are taking the meaning of multiple inheritance in a wrong way. Probably what you have in mind as multiple inheritance is like this:
我认为您以错误的方式理解多重继承的含义。可能你想到的多重继承是这样的:
class A inherits class B
class B inherits class C
If so, then that is wrong. That is not what multiple inheritance is, and Ruby has no problem with that. What multiple inheritance really means is this:
如果是这样,那就错了。这不是多重继承,Ruby 对此没有任何问题。多重继承的真正含义是:
class A inherits class B
class A inherits class C
And you surely cannot do this in Ruby.
你肯定不能在 Ruby 中做到这一点。
回答by Ismael Abreu
No, multi inheritance means one class have more than one parent class. For example in ruby you can have that behavior with modules like:
不,多继承意味着一个类有多个父类。例如,在 ruby 中,您可以使用以下模块来实现该行为:
class Thing
include MathFunctions
include Taggable
include Persistence
end
So in this example Thing class would have some methods from MathFunctions module, Taggable and Persistence, that wouldn't be possible using simple class inheritance.
所以在这个例子中,Thing 类有一些来自 MathFunctions 模块的方法,Taggable 和 Persistence,使用简单的类继承是不可能的。
回答by Nitesh
If class B inherits from class A, then instances of B have the behaviors of both class A and class B
如果类 B 继承自类 A,则 B 的实例具有类 A 和类 B 的行为
class A
end
class B < A
attr_accessor :editor
end
Ruby has single inheritance, i.e. each class has one and only one parent class. Ruby can simulate multiple inheritance using Modules(MIXINs)
Ruby 具有单继承性,即每个类都有一个且只有一个父类。Ruby 可以使用 Modules(MIXINs) 模拟多重继承
module A
def a1
end
def a2
end
end
module B
def b1
end
def b2
end
end
class Sample
include A
include B
def s1
end
end
samp=Sample.new
samp.a1
samp.a2
samp.b1
samp.b2
samp.s1
Module A consists of the methods a1 and a2. Module B consists of the methods b1 and b2. The class Sample includes both modules A and B. The class Sample can access all four methods, namely, a1, a2, b1, and b2. Therefore, you can see that the class Sample inherits from both the modules. Thus you can say the class Sample shows multiple inheritance or a mixin.
模块 A 由方法 a1 和 a2 组成。模块 B 由方法 b1 和 b2 组成。类Sample 包括模块A 和B。类Sample 可以访问所有四种方法,即a1、a2、b1 和b2。因此,您可以看到类 Sample 继承自这两个模块。因此,您可以说 Sample 类显示了多重继承或混合。
回答by Gaurav24
Multiple inheritance- This is absolutely not possible in ruby not even with modules.
多重继承-这在 ruby 中绝对不可能,即使是模块也不行。
multilevel inheritance- This is what is possible even with the modules.
多级继承-即使使用模块,这也是可能的。
Why ?
为什么 ?
Modules acts as an absolute superclasses to the class that includes them.
模块充当包含它们的类的绝对超类。
Ex1 :
例1:
class A
end
class B < A
end
This is a normal inheritance chain, and you can check this by ancestors.
这是一条正常的继承链,你可以通过祖先来检查。
B.ancestors => B -> A -> Object -> ..
B.ancestors => B -> A -> 对象 -> ..
Inheritance with Modules -
继承与模块 -
Ex2 :
例2:
module B
end
class A
include B
end
inheritance chain for above example -
上面例子的继承链 -
B.ancestors => B -> A -> Object -> ..
B.ancestors => B -> A -> 对象 -> ..
Which is exactly same as a Ex1. That means all the methods in module B can override the methods in A, And it acts as a real class inheritance.
这与 Ex1 完全相同。这意味着模块 B 中的所有方法都可以覆盖 A 中的方法,并且它充当真正的类继承。
Ex3 :
例3:
module B
def name
p "this is B"
end
end
module C
def name
p "this is C"
end
end
class A
include B
include C
end
A.ancestors => A -> C -> B -> Object -> ..
A.ancestors => A -> C -> B -> 对象 -> ..
if you see the last example even though two different modules are included they are in a single inheritance chain where B is a superclass of C, C is a superclass of A.
如果您看到最后一个示例,即使包含两个不同的模块,它们也位于单个继承链中,其中 B 是 C 的超类,C 是 A 的超类。
so,
所以,
A.new.name => "this is C"
if you remove the name method from module C, above code will return "this is B". which is same as inheriting a class.
如果从模块 C 中删除 name 方法,上面的代码将返回“这是 B”。这与继承类相同。
so,
所以,
At any point there is only one multilevel inheritance chain in ruby, which nullifies having multiple direct parent to the class.
在任何时候,ruby 中都只有一个多级继承链,这使得class 的多个直接父级无效。

