What is super?

Source: Internet
Author: User

Introduction:

I always thought that the super of OC is the same as the super in Java. I didn't go into its essence until I encountered a situation that could not be explained according to my understanding at work.

Analysis:
Before that, read the following code:
There are two classes: superclass & subclass. Subclass inherits superclass and superclass inherits nsobject.

  @ implementation   subclass -( id  ) init {self  =  [Super init];   If   (Self) {nslog ( @"  %@  " , nsstringfromclass ([Super  class  ]); nslog (  @"  %@  " , nsstringfromclass ([self  class  ]) ;}  @ end  


If you think the two printed results are different, I think you have at least one reason for reading them, because the printed results areSubclass
First, I wrote two high-end functions. I think this is true for the C-language dregs:Id objc_msgsend (ID the receiver, Sel theselector ,...)AndId objc_msgsendsuper (struct objc_super * super, Sel theselector ,...),In fact, this is[Self class]And[Super class].
Let's put it another way: first, I think we can reach a consensus on this point: Selector query mechanism: First, check the list of referers IN THE METHOD list and execute it if any, if no method is found, the method list of the parent class is searched, and so on until the root class is found.
I want to tell you that I have not implemented the class method myself. Based on the inheritance relationship above, I will find this method in the nsobject class and thenObjc_msgsend (ID the receiver, Sel theselector ,...)Because the results are the same, we can see that the aggreger is the same class. In the face of such results, you may wonder, where is super? Why is it the same?

What is super?
Let's look back at the two tall functions. In fact, the so-called message mechanism in OC has seen the first parameter of the second function,Struct objc_super * superOkay, it turns outSuperIt is a struct pointer. Let's see what it is:

StructObjc_super {IDExtends er; Class superclass ;};

 

OriginalSuperContainsCyclerAndCyclerParent classSuperclassIt's hard to imagine[Super class]FinallyObjc_msgsend (objc_super-> extends er, @ selector (class ))This explains why the results are the same. What is the use of superclass In the struct? When querying the list of class functions,[Self class]Is from the current class, and[Super class]The query starts from the parent class of the current class. This is different. AllSuperThe function is to replace the search start point of the function list with the parent class from the current class.

@ Mic

(Email: [email protected])

(Qq: 839720759)

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.