你如何从其他地方调用Objective-C对象的超类的方法? [英] How do you call a method for an Objective-C object's superclass from elsewhere?
问题描述
如果你正在实现一个子类,你可以在你的实现中显式调用超类的方法,即使你覆盖了这个方法,例如:
[self overriddenMethod]; //调用子类的方法
[super overriddenMethod]; //调用超类的方法
如果你想从子类实现之外的地方调用超类的方法,即:
[[object super] overriddenMethod]; // crashes
这是否可能?通过扩展,可以在实现中上升多个级别,即:
[[super super] overriddenMethod ]; //这将工作吗?
一个类别方法,它为你:
- (void)callSuperMethod {[super overriddenMethod]; }
事实上,没有更方便的方法可以做到这一点。 >
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?
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.
这篇关于你如何从其他地方调用Objective-C对象的超类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!