是否可以在重写方法中执行类似 super.super
的操作?也就是绕过直系父辈的super,调用“祖 parent ”的super?
最佳答案
不推荐这样做,但是您想要的是可能像这样:
grandparent = self.class.superclass.superclass
meth = grandparent.instance_method(:the_method)
meth.bind(self).call
它的工作原理是首先获取祖 parent 类,然后对其调用 instance_method
以获得代表祖 parent 版本的 the_method
的 UnboundMethod
。然后它使用 UnboundMethod#bind
和 Method#call
在当前对象上调用祖 parent 的方法。
关于ruby - 调用super的super方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557304/