Objective-c language--self and Super keyword parsing

Source: Internet
Author: User
Tags inheritance

The article turns from

@implementation Son:father
-(ID) init{

    self = [super init];
    if (self) {
    } return
    self;
}

This is an ordinary to initialization method that invokes the Init implementation of the parent class before the subclass implements initialization. Based on this initialization method, we compare the self and Super keywords from three aspects. what self is and what super is.

in the dynamic method : Self represents "object";
in Static methods : Self represents "class";
In a word,self represents the caller of the current method;

Self and super are the two reserved words provided by objective-c. But there is a fundamental difference, self is a hidden parameter variable of the class, pointing to the object (class also object, class object) of the current calling method, and the other hidden parameter is _cmd, representing the selector of the current class method. Super is not a hidden parameter, it is just a "compiler indicator." what [Super Init] did.

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];
Its conversion to

Objc_msgsend (id Self,sel _cmd, ...). Self-> A
At this point, self refers to a object, which starts looking in the method dispatch table of a corresponding class structure, and if it is not found, the deferred inheritance chain is searched for in the parent class.

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

Class  A
-reposition{...  
     [Super SetOrigin:someX:someY];  
     ...  
}

[A reposition]; The compiler in the method body will

[Super SetOrigin:someX:someY];
Its conversion to

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

The first argument is a objc_super structure, and the second argument is the selector of the class method above, look at what the Objc_super is like:

struct Objc_super {
ID receiver;
Class superclass;
};

You can see that this structure contains two members, one is receiver, this is similar to the first parameter of the objc_msgsend above receiver, the second member is record write Super What is the parent of the class, take the above code for example, when the compiler encounters a

[Super SetOrigin:someX:someY]
, start doing these things:

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

Call the Objc_msgsendsuper method to put this struct and

Setorigin
The SEL passes past. The function is doing something like this: starting from the list of superclass methods pointed to by the objc_super structure, start looking for the selector of Setorigin, and then objc_super->receiver to call this selector, The Objc_msgsend function may also be used, but the first argument at this point Thereceiver is Objc_super->receiver and the second parameter is found from Objc_super->superclass why selector to self = [super init];

Comply with OC Inheritance Class initialization specification Super also, [Super Init] goes to self's super to invoke Init super to invoke 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 attributes to it, returning the memory pointer, so that the memory pointer that is being initialized by the inheritance chain is passed from top to bottom, and the necessary attributes are added to block memory in different subclasses until our A class to get the memory pointer, assign it to the Slef parameter, and in the 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 be easy to analyze the print results:

Son
Son

When sending a class message, either 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, a superclass from this class, one from this class.
In general, the class method is only defined in the root class nsobject, and there are few instances where subclasses override the class method.
So [Slef class] and [Super class] are all in the root class to find the method implementation, the message receiving body is also a
If the override might be different.
Naturally all print out Son

In an example to come:

#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];//directly call the parent class method, engine does not overload it

[Engine  printsupclass];//indirectly invokes the parent class method,

Printing is of course all:

Engine
Engine

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

Super is a blind hair, compiler symbol, which can be replaced by [Slef class], but the method is to start looking from the superclass of self.

Related Article

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.