San*_*ago 5 ruby inheritance module
我有两个模块具有相同的方法名称.当我在某个类中包含两个模块时,只执行最后一个模块的方法.我需要在初始化类时执行它们:
class MyClass
include FirstModule
include SecondModule
def initialize
foo # foo is contained in both modules but only the one in SecondModules is executed
end
end
Run Code Online (Sandbox Code Playgroud)
它可行吗?
Bor*_*cky 10
正如Yusuke Endoh所说,Ruby中的一切都是可行的.在这种情况下,你必须忘记只是说"foo"的便利性,你必须非常清楚你真正想做的事情,比如:
class MyClass
include FirstModule
include SecondModule
def initialize
FirstModule.instance_method( :foo ).bind( self ).call
SecondModule.instance_method( :foo ).bind( self ).call
end
end
Run Code Online (Sandbox Code Playgroud)
"FirstModule.instance_method ......"这一行可以简单地用"foo"代替,但是通过明确表示,无论如何,你都要确保从混合中调用方法,你认为这样做.
小智 7
你能修改包含的模块吗?也许你只是打电话super给第二个模块?
module M1
def foo
p :M1
end
end
module M2
def foo
p :M2
defined?(super) && super
end
end
class SC
include M1
include M2
def initialize
foo
end
end
SC.new
Run Code Online (Sandbox Code Playgroud)
或者你真的想要这样做?
module M1
def bar; p :M1 end
end
module M2
include M1
def foo; bar; p :M2 end
end
class SC
include M2
def initialize; foo end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3483 次 |
| 最近记录: |