The difference between self and super

Source: Internet
Author: User

About the difference between self and super,

The first thing to know about 1 is what self is and what super is. What 2,[Super init] does. 3, why should self = [super init];

What is 1,self and what is super?

> In dynamic methods, self represents an "object"

> In static methods, self stands for "class"

> Original aim, just remember a word: Self represents the caller of the current method

Self and super are the two reserved words that OC provides. But there is a fundamental difference,

Self is a hidden parameter variable of the class, pointing to the object that is currently calling the method (the class is also an object, the class object), and the other hidden parameter is _cmd, which represents the selector of the current class method.

Super is not a hidden parameter, it's just a "compiler indicator"

2, [Super Init] what to do

When sending a message

Class  A
-reposition { ... [Self SetOrigin:someX:someY]; ... }
A a= [A.. init];
[A reposition]; The compiler in the method body will
  [Self SetOrigin:someX:someY];  

Convert it to

Objc_msgsend (self,_cmd,.. .) 。      self-A

At this point, self refers to the A object, and the method starts looking in a method schedule table corresponding to a class structure, and if it is not found, the inheritance chain is looked for in the parent class.

Similarly, if reposition is a class method, self refers to the class A object.

Class  a-reposition  {       ...       [Super SetOrigin:someX:someY];       ...  }
[A reposition]; The compiler in the method body will

Convert it to

ID objc_msgsendsuper (struct objc_super *super, SEL op, ...)

The first parameter is a objc_super structure, the second parameter is similar to the selector of the class method above, first look at objc_super what this struct is:

struct Objc_super {
ID receiver;
Class superclass;
};

You can see that this struct contains two members, one is receiver, this is similar to the first parameter receiver of the above Objc_msgsend, the second member is what is the parent class that records the Super class, take the code above as an example, when the compiler encounters A

[Super SetOrigin:someX:someY]

, start doing these things:

> constructs the objc_super structure, at which point the first member variable receiver of this struct is a, which is the same as self. The second member variable, superclass, refers to the superclass of Class A.

> Call the Objc_msgsendsuper method to make this struct and

Setorigin

The SEL passes past. The function inside is doing something like this: from the Superclass method list of the objc_super structure to find Setorigin selector, find and then objc_super->receiver to call this selector, The Objc_msgsend function may also be used, but the first parameter thereceiver is Objc_super->receiver, and the second argument is found from Objc_super->superclass Selector

3, why should self = [super init];

Conforms to the OC Inheritance Class initialization specification Super too, [Super Init] goes to self's super to call Init super to call Supersuper's init. Until init in the root class NSObject,

In the root class, Init is responsible for initializing the memory area to add some necessary properties to the inside, returning the memory pointer, so that the memory pointer that is initialized by the inheritance chain is passed from top to bottom, and the necessary attributes of the subclass are added to the block memory in different subclasses until we get the memory pointer in class A, assign the value to the Slef parameter Number, in if (Slef) {//Add A's properties}

Here's a look at this:

@implementation son:father-(ID) init{self    = [super init];    if (self)    {        NSLog (@ '%@ ', Nsstringfromclass ([Self class]));        NSLog (@ "%@", Nsstringfromclass ([Super Class]);    }    return self;} @end

It should not be difficult to analyze the printed result:

Son
Son

When sending a class message, whether self or super, the message body remains self , meaning that self and super point to the same object. Just find the location difference of the method, one from this class, and one from the superclass of this class.
In general, the class method is defined only in the root class NSObject, and very few subclasses rewrite the class method.
So [Slef class] and [Super class] Both find the method implementation in the root class, and the message receiving body is both a
If the override may be different.
Naturally all print out

In an example:
#import <Foundation/Foundation.h> @interface enginesuper:nsobject-(void) printcurrentclass; @end #import " EngineSuper.h "@implementation enginesuper-(void) printcurrentclass{    NSLog (@" =enginesuper=======%@ ", [Self Class]);} @end @interface engine:enginesuper-(void) printsupclass; @end @implementation engine-(void) printsupclass{   [Super Printcurrentclass];} Call: Engine *engine = [[Engine alloc]init]; [Engine  printcurrentclass];//calls the parent class method directly, the engine does not reload it [engine  printsupclass];//indirectly calls the parent class method,

Printing is of course:

Engine
Engine

The self in the method body always refers to the recipient of the method and the object engine. ,
Change to   NSLog (@ "=enginesuper=======%@", [Super class]), and the result is the same.

The first major points:

    1. Self calls its own method, super calls the parent class method
    2. Self is a class, super is a precompiled instruction
    3. The "Self Class" and "super class" outputs are the same

Self and Super Bottom implementation principles:

    1. When the method is called with self, it is looked up from the list of methods in the current class, if not, from the parent class, and when super is used, the parent class is called from the list of methods, and then the method for the parents is invoked.
    2. When called with self, the Objc_msgsend function is used: ID objc_msgsend (ID thereceiver, SEL theselector, ...). The first parameter is the message receiver, and the second parameter is the selector of the concrete class method called, followed by the mutable arguments of the selector method.  with [self setName:] As an example, the compiler replaces the function call called Objc_msgsend, where Thereceiver is the Self,theselector @selector (setName:), which is selector from the current The Self's class method list starts looking for the setName, and when found, passes the corresponding selector to the past.
    3. When using super call, the Objc_msgsendsuper function is used: ID objc_msgsendsuper (struct objc_super *super, SEL op, ...) The first parameter is a objc_super struct, and the second parameter is a selector similar to the class method above.
struct Objc_super {
ID receiver;
Class superclass;
};

When the compiler encounters [Super SetName:], start doing these things:

1) Construct the structure of the Objc_super, at which point the first member variable receiver of the struct is the subclass, which is the same as self. The second member variable, superclass, refers to the parent class
Call the Objc_msgsendsuper method to pass the structure and the SetName sel over.
2) The function inside is doing something like this: from the objc_super structure point to the Superclass method list to find SetName selector, find and then objc_super->receiver to call this selector

The difference between self and super

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.