如何从其他地方调用 Objective-C 对象超类的方法?

发布于 2024-09-02 00:33:02 字数 414 浏览 5 评论 0原文

如果您正在实现一个子类,您可以在您的实现中显式调用超类的方法,即使您已经覆盖了该方法,即:

[self overriddenMethod]; //calls the subclass's method
[super overriddenMethod]; //calls the superclass's method

如果您想从子类实现之外的某个地方调用超类的方法,即:

[[object super] overriddenMethod]; //crashes

这可能吗?通过扩展,是否有可能在实现中上升不止一个级别,即:

[[super super] overriddenMethod]; //will this work?

If you're implementing a subclass, you can, within your implementation, explicitly call the superclass's method, even if you've overridden that method, i.e.:

[self overriddenMethod]; //calls the subclass's method
[super overriddenMethod]; //calls the superclass's method

What if you want to call the superclass's method from somewhere outside the subclass's implementation, i.e.:

[[object super] overriddenMethod]; //crashes

Is this even possible? And by extension, is it possible to go up more than one level within the implementation, i.e.:

[[super super] overriddenMethod]; //will this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-09-09 00:33:02

一个更令人愉快的选择(用于调试)是添加一个为您执行此操作的类别方法:

- (void) callSuperMethod { [super overriddenMethod]; }

事实上,没有更方便的方法来执行此操作,这很大程度上是设计使然。

A more pleasant option (for debugging) is to add a category method which does it for you:

- (void) callSuperMethod { [super overriddenMethod]; }

The fact that there’s no more convenient way to do this is very much by design.

风吹雨成花 2024-09-09 00:33:02

如果您愿意,您可以发送 [[[object class] superclass] methodForSelector: ...] 并直接通过 IMP 指针调用...但如果您这样做,可能会出现一些非常严重的情况您的应用程序的设计有问题。

You can send [[[object class] superclass] methodForSelector: ...] and invoke through the IMP pointer directly, if you want... but if you're doing this, there's probably something very wrong with your application's design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文