到底什么是“超级”?在我的应用程序中做什么?
好吧,这将是一个非常尴尬的问题,但在我的初学者书籍中(尤其是那些通常有用的索引),我看起来很生气,而且我没有找到关于“超级”在我的书中的作用的实际解释。应用程序。
我查看了这里,但恐怕那是远远超出了我的范围。谁能简单地解释一下“超级”在我的应用程序中的作用以及为什么我应该与它成为朋友?例如:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
super
调用超类的方法实现。因此,如果您的类继承了UIViewController
,那么[super viewDidLoad];
将调用UITableViewController
类的viewDidLoad
方法。您通常应该这样做,因为超类可能会执行需要发生的重要事情。就 viewDidLoad 而言,我不确定它当前是否确实执行了任何操作,但在框架的未来版本中它总是可以执行。super
calls the superclass's implementation of the method. So if your class inheritsUIViewController
, then[super viewDidLoad];
will call theUITableViewController
class'sviewDidLoad
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.在底层,
self
包含一个指向它响应的 set 方法的指针。这是实现动态调度的基本机制。每个班级都有一套独特的套装。如果您熟悉 C++,这在概念上与虚拟表类似。您可以将 objc 方法视为具有 2 个隐藏参数 (self,_cmd) 的 C 函数。super
是动态创建的self
表示,带有指向实例超类实现的下一个内联方法的指针。该表示基于self
,并且仅指向另一组已实现的方法。如果您创建
MONTypeA
的实例,那么它将响应monMethod
:因此,调用
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 ofself
, with a pointer to the next-in-line methods implemented by the instance's superclasses. this representation is based onself
, and just directs to another set of implemented methods.if you create an instance of
MONTypeA
, then it will respond tomonMethod
: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).