Self, superclass, superself: Caller of the current method class: Gets the class object of the method caller superclass: Gets the parent class object of the method caller super: Not a pointer, a compilation indicator (identifier), Special handling is done internally during program compilation
super的本质:其实还是当前对象去调用,只不过让当前对象去调用父类方法, super不是父类对象,指的是父类方法。
Verify:
Define a person class in a main.m file, overriding the description method.
#import<objc/message.h> @interface Span class= "Hljs-title" >person: nsobject @end @implementation person-( nsstring *) description{//Super: Current object calls //Call NSObject method return [super description];} @end int Main (int argc, const char * argv[]) { @autoreleasepool { } return 0;}
The terminal uses the CD command to jump to the MAIN.M directory, and then enter: CLANG-REWRITE-OBJC main.m command, you can convert the main.m file to a mian.cpp file, open the file at the end of the file can see [super Description] The underlying implementation of this code is as follows:
((NSString *(*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("Person"))}, sel_registerName("description"));
Simplify the code above to force type conversions after the code is as follows:
objc_msgSendSuper({self, class_getSuperclass(objc_getClass("Person"))}, sel_registerName("description"))
where {Self,class_getsuperclass (Objc_getclass ("person"))}j is the underlying implementation of super, the method of calling the parent class using super is still essentially the method that the current object calls the parent class.
VIEWCONTROLLER.M File-(void) Viewdidload {Create Subperson Subperson *SUBP = [[Subperson alloc] init]; [SUBP test];}//person.m file-(void) test{ NSLog (@ "%@%@%@%@", [Selfclass], [selfsuperclass], [Super Class ], [Super Superclass]);} //subperson.m file-(void) test{ //test 1 NSLog (@ "%@%@%@%@", [Selfclass], [selfsuperclass ], [Super Class], [Super Superclass]); //Test 2 [Super Test];}
Test 1 Prints the result: Subperson person Subperson person
Test 2 Prints The result: Subperson person Subperson person
The difference between self, superclass, super