[Objective-C] details about object creation, self, and super in OC

Source: Internet
Author: User

1: self is a hidden parameter of the class, pointing to the class of the method currently called, and another hidden parameter is _ cmd, representing the selector of the current class method.

2: super is not a hidden parameter. It is just a "compiler indicator", and it points to the same message recipient as self.

3: When self is used to call a method, it will start from the method list of the current class. If no, it will be found from the parent class. When super is used, find the method from the method list of the parent class, and then call the method of the parent class.

 

For details, refer to the code implementation method (For details, refer to the comments in the Code)

I. view the PersonSelf. h declaration File

 

/// PersonSelf. h // PersonSelf // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import
     
      
@ Class Student; // @ class is generally used in the header file @ interface PersonSelf: NSObject {// declare two instance variables int age; NSString * name; Student * s ;} // define the method-(NSString *) getName;-(void) setName :( NSString *) _ name;-(int) getAge;-(void) setAge :( int) _ age; @ end
     
Ii. PersonSelf. m implementation file

 

 

/// PersonSelf. m // PersonSelf // Created by hmjiangqq on 14-1-22. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import "PersonSelf. h "# import" Student. h "// # import is generally used in the implementation file @ implementation PersonSelf-(id) init {if (self = [super init]) {// super can call the method s = [[Student alloc] init] in the parent class; NSLog (@ "student: % @ \ n", s);} return self ;} -(NSString *) getName {return name;}-(void) setName :( NSString *) _ name {name = _ name; [self setAge: 14]; // self can call the method NSLog (@ "setAge") in this class object; NSLog (@ "self class is: % @ \ n", [self class]); NSLog (@ "super class is: % @ \ n", [super superclass]) ;}- (int) getAge {return age ;}- (void) setAge :( int) _ age {age = _ age ;}@ end

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.