rails ruby 中的模块和多重继承?
Modules and multiple inheritance in ruby on rails?
我在 rails 上了解到 ruby 中的模块表现为多重继承。我看到了一个我理解的示例,但是如果两个模块具有相同的方法那么调用哪个呢?
module A
def a1
end
def a2
end
end
module B
def a1
end
def b2
end
end
class Sample
include A
include B
def s1
end
end
samp=Sample.new
samp.a1
samp.a1
最后获胜。在您的示例中(您可以在控制台上使用 print 语句自己尝试 运行 它)将调用的 a1
方法是 B
.[= 中定义的方法12=]
Ruby 没有多重继承。 Ruby 有类似的东西叫做 mixins,可以使用模块实现。
Mixins 不是多重继承,而是主要消除了对它的需求。
为了回答你的问题,当你在 class 中包含两个模块并且它们都有同名方法(在你的例子中,方法 a
),在这种情况下,a
将调用第二个(最后一个)模块的方法。
module A
def a1
puts "I am defined in A"
end
def a2
end
end
module B
def a1
puts "I am defined in B"
end
def b2
end
end
class Sample
include A
include B
def s1
end
end
samp = Sample.new
puts samp.a1
# I am defined in B
为什么以及如何?
When a module M is included in a class C, an anonymous proxy class
⟦M′⟧ (called an include class) is created such that its method table
pointer points to M's method table. (Same for the constant table and
module variables.) ⟦M′⟧'s superclass is set to C's superclass and C's
superclass is set to ⟦M′⟧.
此外,如果 M 包含其他模块,则递归应用该过程。
puts Sample.ancestors.inspect
# [Sample, B, A, Object, Kernel, BasicObject]
当您检查 Sample
class 的 ancestors
时会变得更加清楚。请参阅此处的顺序。当调用a
方法时,Ruby首先在Sample
class本身中查找方法定义,但没有找到。然后,它在 B
中查找 a
方法并找到并调用它。
希望你现在清楚了。
我在 rails 上了解到 ruby 中的模块表现为多重继承。我看到了一个我理解的示例,但是如果两个模块具有相同的方法那么调用哪个呢?
module A
def a1
end
def a2
end
end
module B
def a1
end
def b2
end
end
class Sample
include A
include B
def s1
end
end
samp=Sample.new
samp.a1
samp.a1
最后获胜。在您的示例中(您可以在控制台上使用 print 语句自己尝试 运行 它)将调用的 a1
方法是 B
.[= 中定义的方法12=]
Ruby 没有多重继承。 Ruby 有类似的东西叫做 mixins,可以使用模块实现。
Mixins 不是多重继承,而是主要消除了对它的需求。
为了回答你的问题,当你在 class 中包含两个模块并且它们都有同名方法(在你的例子中,方法 a
),在这种情况下,a
将调用第二个(最后一个)模块的方法。
module A
def a1
puts "I am defined in A"
end
def a2
end
end
module B
def a1
puts "I am defined in B"
end
def b2
end
end
class Sample
include A
include B
def s1
end
end
samp = Sample.new
puts samp.a1
# I am defined in B
为什么以及如何?
When a module M is included in a class C, an anonymous proxy class ⟦M′⟧ (called an include class) is created such that its method table pointer points to M's method table. (Same for the constant table and module variables.) ⟦M′⟧'s superclass is set to C's superclass and C's superclass is set to ⟦M′⟧.
此外,如果 M 包含其他模块,则递归应用该过程。
puts Sample.ancestors.inspect
# [Sample, B, A, Object, Kernel, BasicObject]
当您检查 Sample
class 的 ancestors
时会变得更加清楚。请参阅此处的顺序。当调用a
方法时,Ruby首先在Sample
class本身中查找方法定义,但没有找到。然后,它在 B
中查找 a
方法并找到并调用它。
希望你现在清楚了。