简体   繁体   English

调用超级方法时跳过超类?

[英]Skip a super class when calling a super method?

This may seem strange, but I would like to do the following: 这可能看起来很奇怪,但我想做以下事情:

Class A A级

- (void)someMethod;

Class B : A B级:A

- (void)someMethod; // overrides

Class C : B C级:B

- (void)someMethod; // overrides

In class C, I would like to override a method present in the two super classes, but call only Class A's method on a [super methodName] call. 在C类中,我想覆盖两个超类中存在的方法,但是在[super methodName]调用上只调用A类的方法。

- (void)someMethod
{
  [super someMethod];  // but I want to call class A, not B
}

Possible? 可能?

Something like this should work: 这样的事情应该有效:

// in Class C:
#import <objc/runtime.h>

- (void)someMethod
{
    // I want to call class A's implementation of this method

    IMP method = class_getMethodImplementation([ClassA class], _cmd);

    method(self, _cmd);

}

Well, you could do a hacky solution whereby you pass an integer parameter into the method which is decremented on each super call, such that each method implementation just calls its superclass method until finally you reach 0, then you actually execute the method. 好吧,你可以做一个hacky解决方案,你可以将一个整数参数传递给每个超级调用递减的方法,这样每个方法实现只调用它的超类方法直到最后达到0,然后你实际执行该方法。 It's sort of like a weird form of recursion. 这有点像一种奇怪的递归形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM