The difference between self, superclass, super

Source: Internet
Author: User

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

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.