我有两节课A
,和B
.类B
重写了类的foo
方法A
.Class B
有一个bar
方法,我想调用foo
超类的方法.这种电话的语法是什么?
class A
def foo
"hello"
end
end
class B < A
def foo
super + " world"
end
def bar
# how to call the `foo` method of the super class?
# something similar to
super.foo
end
end
Run Code Online (Sandbox Code Playgroud)
对于类方法,我可以通过显式地为类名添加前缀来在继承链上调用方法.我想知道是否有类似的习惯用法.
class P
def self.x
"x"
end
end
class Q < P
def self.x
super + " x"
end
def self.y
P.x
end
end
Run Code Online (Sandbox Code Playgroud)
编辑
我的用例是一般的.对于特定情况,我知道我可以使用alias
技术.这是Java或C++中的常见功能,所以我很想知道是否可以在不添加额外代码的情况下执行此操作.
小智 33
在Ruby 2.2中,您Method#super_method
现在可以使用
例如:
class B < A
def foo
super + " world"
end
def bar
method(:foo).super_method.call
end
end
Run Code Online (Sandbox Code Playgroud)
参考:https://bugs.ruby-lang.org/issues/9781#change-48164和https://www.ruby-forum.com/topic/5356938
Son*_*tos 23
你可以做:
def bar
self.class.superclass.instance_method(:foo).bind(self).call
end
Run Code Online (Sandbox Code Playgroud)
您可以alias old_foo foo
在重新定义之前将旧实现保留在新名称下.(从技术上讲,有可能采用超类的实现并将其绑定到子类的实例,但它很笨拙,并非完全没有惯用,并且在大多数实现中可能都很慢.)
归档时间: |
|
查看次数: |
37501 次 |
最近记录: |