如何调用super.super的方法? [英] How to call a method of super.super?
问题描述
我想调用一个超类的超类的方法,而不打破继承链。像这样:
+(id)alloc
{
return [super.super alloc];有没有办法实现这一点?
不要混淆由超类
方法提供的行为,这里。
UPD: p>
关于 super
和超类
p>
假设我们有 AClass
和 SuperAClass
。如下从他们的名称 AClass
继承 SuperAClass
。每个都有一个方法的实现 - (void)foo;
AClass
实现以下类方法之一:
1。超类:
+(id)alloc {
return [[self superclass] alloc];
}
2。 super:
+(id)alloc {
return [super alloc]
}
现在,假设这两行代码:
AClass * AClassInstance = [AClass alloc];
[AClassInstance foo];
在第一种情况下(使用超类), SuperAClass
将调用 foo
方法。
对于第二种情况(使用super), AClass
的 foo
方法将被调用。 p>
解决方案在你的具体例子中, +超类
+(id)someClassMethod {
return [[[self superclass] superclass] someClassMethod];
}
因为它是一个类方法,因此 self
是指定义 + someClassMethod
的类对象。
另一方面,事情在实例方法中变得更加复杂。一个解决方案是获取一个指向超级(祖父级)类中的方法实现的指针。例如:
- (id)someInstanceMethod {
Class granny = [[self superclass] superclass];
IMP grannyImp = class_getMethodImplementation(granny,_cmd);
return grannyImp(self,_cmd);
}
与类方法示例类似, + superclass
发送两次以获取超级超类。 IMP
是一个指向一个方法的指针,我们获取一个IMP到该方法的名称与当前方法相同( -someInstaceMethod
)但是指向在超类中的实现,然后调用它。注意,如果有方法参数和返回值不同于 id
,那么您需要调整此方法。
I want to call a method of super class of a super class, without breaking the inheritance chain. Something like this:
+(id) alloc
{
return [super.super alloc];
}
Is there a way to achieve this ?
Do not confuse with behavior offering by superclass
method, discussed here.
UPD:
A few words about super
and superclass
differences.
Lets say, we have AClass
and SuperAClass
. As follows from their names AClass
inherits SuperAClass
. Each of them has an implementation of a method -(void) foo;
AClass
implements one of the following class methods:
1. superclass:
+(id) alloc {
return [[self superclass] alloc];
}
2. super:
+(id) alloc {
return [super alloc];
}
Now, suppose these 2 lines of code:
AClass *AClassInstance = [AClass alloc];
[AClassInstance foo];
In first case (using superclass), SuperAClass
's foo
method will be called.
For the second case (using super), AClass
's foo
method will be called.
解决方案 In your particular example, +superclass
is actually the way to go:
+ (id)someClassMethod {
return [[[self superclass] superclass] someClassMethod];
}
since it is a class method, hence self
refers to the class object where +someClassMethod
is being defined.
On the other hand, things get a tad more complicated in instance methods. One solution is to get a pointer to the method implementation in the supersuper (grandparent) class. For instance:
- (id)someInstanceMethod {
Class granny = [[self superclass] superclass];
IMP grannyImp = class_getMethodImplementation(granny, _cmd);
return grannyImp(self, _cmd);
}
Similarly to the class method example, +superclass
is sent twice to obtain the supersuperclass. IMP
is a pointer to a method, and we obtain an IMP to the method whose name is the same as the current one (-someInstaceMethod
) but pointing to the implementation in the supersuperclass, and then call it. Note that you’d need to tweak this in case there are method arguments and return values different from id
.
这篇关于如何调用super.super的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!