可可爷爷

发布于 2024-09-27 16:00:51 字数 394 浏览 4 评论 0原文

是否可以访问对象超类(或祖父)的超类方法?

例如:

GrandFatherObject : NSObject
SuperObject : GrandFatherObject
SelfObject : SuperObject

来自 SelfObject:

- (void)overriddenMethod
{
  // For Self
  someCode();

  // For Parent
  [super overriddenMethod];

  // For GrandParent
  ???
}

我只能访问 SelfObject(无法修改 SuperObject 或 GrandFatherObject)

Is it possible to access the super class method of an objects super class (or grandfather)?

For instance:

GrandFatherObject : NSObject
SuperObject : GrandFatherObject
SelfObject : SuperObject

From SelfObject:

- (void)overriddenMethod
{
  // For Self
  someCode();

  // For Parent
  [super overriddenMethod];

  // For GrandParent
  ???
}

I only have access to SelfObject (Can't modify SuperObject or GrandFatherObject)

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

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

发布评论

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

评论(3

甜宝宝 2024-10-04 16:00:51

是的,您可以做到这一点,但它需要的代码比仅仅调用 super 多一点。

或多或少,它会是这样的:

#import <objc/runtime.h>

struct objc_super grandsuper;
grandsuper.receiver = self;
grandsuper.class = class_getSuperclass(class_getSuperclass([self class]));

//if _cmd has a non-struct return value:
id grandsuperReturnValue = objc_msgSendSuper(&grandsuper, _cmd, arg1, arg2, ...);

Yes, you can do it, but it takes a bit more code than just invoking super.

More or less, it'd be something like this:

#import <objc/runtime.h>

struct objc_super grandsuper;
grandsuper.receiver = self;
grandsuper.class = class_getSuperclass(class_getSuperclass([self class]));

//if _cmd has a non-struct return value:
id grandsuperReturnValue = objc_msgSendSuper(&grandsuper, _cmd, arg1, arg2, ...);
凉世弥音 2024-10-04 16:00:51

你为什么要这样做?如果您认为有必要,您应该重新考虑您的类设计。当然,您始终可以在父类的 overriddenMethod 实现中调用 super

为了正确封装,子类不需要了解除父类之外的任何信息。否则,继承层次结构中就会出现紧密的双向耦合。噩梦。

Why would you want to do this? You should rethink your class design if you think this is necessary. You can always, of course, call super in the parent class's implementation of overriddenMethod.

For proper encapsulation, subclasses shouldn't need to know about anything other than their parent class. Otherwise you have tight, bi-directional coupling within the inheritance hierarchy. Nightmares.

缱倦旧时光 2024-10-04 16:00:51

虽然使用 objc_msgSendSuper() 当然很有趣,但为什么不采取简单的方法并在 SelfObject 实现中创建一个类别,就像

@interface SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod;
@end

@implementation SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod {
    return [super overridenMethod];
  }
@end

你说你不能修改 SuperObject,这并不意味着你不能扩展它。我在这里遗漏了任何主要缺点吗?

While playing with objc_msgSendSuper() is certainly fun, why don't you take the easy way and create a Category in the SelfObject implementation, something like

@interface SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod;
@end

@implementation SuperObject (ForSelfObject)
  - (id)grandFatherOverridenMethod {
    return [super overridenMethod];
  }
@end

You said you can't modify SuperObject, that doesn't mean you can't extend it. Am I missing any major drawbacks here?

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