到底什么是“超级”?在我的应用程序中做什么?

发布于 2024-10-21 16:41:54 字数 339 浏览 6 评论 0原文

好吧,这将是一个非常尴尬的问题,但在我的初学者书籍中(尤其是那些通常有用的索引),我看起来很生气,而且我没有找到关于“超级”在我的书中的作用的实际解释。应用程序。

我查看了这里,但恐怕那是远远超出了我的范围。谁能简单地解释一下“超级”在我的应用程序中的作用以及为什么我应该与它成为朋友?例如:

- (void)viewDidLoad {
[super viewDidLoad];}

谢谢!!

Ok, this is going to be a really embarrassing question, but I looked like mad in my beginner's books (especially those usually helpful indices) and behold, I did not find a down-to-earth explanation of what 'super' does in my applications.

I had a look at this here, but I'm afraid that's well beyond me. Can anyone offer a very simply explanation of what 'super' does in my apps and why I should become friends with it? E.g.:

- (void)viewDidLoad {
[super viewDidLoad];}

Thanks!!

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

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

发布评论

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

评论(2

赴月观长安 2024-10-28 16:41:54

super 调用超类的方法实现。因此,如果您的类继承了 UIViewController,那么 [super viewDidLoad]; 将调用 UITableViewController 类的 viewDidLoad 方法。您通常应该这样做,因为超类可能会执行需要发生的重要事情。就 viewDidLoad 而言,我不确定它当前是否确实执行了任何操作,但在框架的未来版本中它总是可以执行。

super calls the superclass's implementation of the method. So if your class inherits UIViewController, then [super viewDidLoad]; will call the UITableViewController class's viewDidLoad method. You should usually do this because the super class may do important things that need to happen. In the case of viewDidLoad, I'm not sure it actually does anything currently, but it always could in a future version of the framework.

蓦然回首 2024-10-28 16:41:54

在底层,self 包含一个指向它响应的 set 方法的指针。这是实现动态调度的基本机制。每个班级都有一套独特的套装。如果您熟悉 C++,这在概念上与虚拟表类似。您可以将 objc 方法视为具有 2 个隐藏参数 (self,_cmd) 的 C 函数。

super 是动态创建的 self 表示,带有指向实例超类实现的下一个内联方法的指针。该表示基于 self,并且仅指向另一组已实现的方法。

@interface MONTypeA : NSObject
- (void)monMethod;
@end

@interface MONTypeB : MONTypeA
- (void)monMethod;
@end

@implementation MONTypeA

- (void)monMethod {
    printf("i am MonTypeA\n");
}

@end

@implementation MONTypeB

- (void)monMethod {
    [super monMethod]; /* << will call -[MONTypeA monMethod], which will print "i am MonTypeA\n" */
    printf("i am MonTypeB\n");
}

@end

如果您创建 MONTypeA 的实例,那么它将响应 monMethod

MONTypeA * a = [MONTypeA new];
[a monMethod];
[a release], a = 0;

// outputs: "i am MonTypeA\n"

MONTypeB * b = [MONTypeB new];
[b monMethod];
[b release], b = 0;

// outputs: "i am MonTypeA\n""i am MonTypeB\n" because -[MONTypeB monMethod] calls through super

因此,调用 super 执行超类方法的实现这个具体案例。

重要的是要记住:super 引用的方法集始终是层次结构中前一个该方法的实现的方法,而不是 > 与将给出超类实例的实例方法集相同的集合(除非您的类要重写每个方法)。

at the low level, self contains a pointer to the set methods it responds to. this is a basic mechanism for the implementation of dynamic dispatch. each class is given a unique set. if you're familiar with c++, this is similar in concept to a virtual table. you can think of an objc method as being like a C function with 2 hidden arguments (self,_cmd).

super is a dynamically created representation of self, with a pointer to the next-in-line methods implemented by the instance's superclasses. this representation is based on self, and just directs to another set of implemented methods.

@interface MONTypeA : NSObject
- (void)monMethod;
@end

@interface MONTypeB : MONTypeA
- (void)monMethod;
@end

@implementation MONTypeA

- (void)monMethod {
    printf("i am MonTypeA\n");
}

@end

@implementation MONTypeB

- (void)monMethod {
    [super monMethod]; /* << will call -[MONTypeA monMethod], which will print "i am MonTypeA\n" */
    printf("i am MonTypeB\n");
}

@end

if you create an instance of MONTypeA, then it will respond to monMethod:

MONTypeA * a = [MONTypeA new];
[a monMethod];
[a release], a = 0;

// outputs: "i am MonTypeA\n"

MONTypeB * b = [MONTypeB new];
[b monMethod];
[b release], b = 0;

// outputs: "i am MonTypeA\n""i am MonTypeB\n" because -[MONTypeB monMethod] calls through super

therefore, calling super performs the implementation of the method of the superclass in this specific case.

it is important to remember: the set of methods super refers to is always those of the previous implementation of the method in the hierarchy, it is not the same set as the set of instance methods which an instance of the superclass would be given (unless your class were to override every method).

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