调用 super 的 super 方法 [英] Calling super's super method

查看:59
本文介绍了调用 super 的 super 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在覆盖方法中执行类似 super.super 的操作?也就是说,绕过直系父母的super,调用祖父母的"super?

Is is possible to do something like super.super in the overriden method? That is, to bypass the direct parent's super and call "grandparent's" super?

推荐答案

这是不推荐的,但是你想要的可能的:

This is not recommended, but what you want is possible like this:

grandparent = self.class.superclass.superclass
meth = grandparent.instance_method(:the_method)
meth.bind(self).call

首先获取祖父类,然后对其调用instance_method 以获取表示the_method 祖父版本的UnboundMethod.然后它使用 UnboundMethod#bindMethod#call 在当前对象上调用祖父的方法.

This works by first getting the grandparent class, then calling instance_method on it to get an UnboundMethod representing the grandparent's version of the_method. It then uses UnboundMethod#bind and Method#call to call the grandparent's method on the current object.

这篇关于调用 super 的 super 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆