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 |