Objective-C Category

Source: Internet
Author: User

Objective-C Category
OC provides a function to extend the class method without modifying the source code of the class, "Category" Category. This mechanism is similar to the extension method in C, it is generally used when it is not convenient to modify the source code of the class (such as the class library related to the OC system), but there is a need to expand the class method, you can use the classification to achieve.

Java should not provide relevant mechanisms, but Java can implement AOP through dynamic proxy and execute code logic before and after the starting point. I personally think it is more convenient than OC.

/// Girl. h // 10_Category /// Created by apple on 14/12/23. // Copyright (c) 2014 cc. All rights reserved. // # import
 
  
@ Interface Girl: NSObject // display the Chinese name of MM-(NSString *) showEnglishName; @ end /// Girl. m // 10_Category /// Created by apple on 14/12/23. // Copyright (c) 2014 cc. all rights reserved. // # import "Girl. h "@ implementation Girl-(NSString *) showEnglishName {return @" mm ";}@ end /// Girl + GirlCategory. h // 10_Category /// Created by apple on 14/12/23. // Copyright (c) 2014 cc. all rights reserved. // # import "Girl. h "// header file for OC classification // naming convention base class + category name. h /. m // format @ interface base class (Category) // category: without modifying the base class source file, dynamic Extension Method for the base class // you can add some extension methods for these classes without modifying the source code of the OC or IOS framework by Category. // classification is not a subclass inheritance method, instead, it directly extends the base class @ interface Girl (GirlCategory) // adds a method for displaying the Chinese name to the Girl class-(NSString *) showChineseName; @ end // Girl + GirlCategory. m // 10_Category /// Created by apple on 14/12/23. // Copyright (c) 2014 cc. all rights reserved. // # import "Girl + GirlCategory. h "@ implementation Girl (GirlCategory)-(NSString *) showChineseName {return @" Meimei ";}@ end // main. m // 10_Category /// Created by apple on 14/12/23. // Copyright (c) 2014 cc. all rights reserved. // # import
  
   
# Include "Girl. h "# include" Girl + GirlCategory. h "int main (int argc, const char * argv []) {@ autoreleasepool {Girl * pGirl = [[Girl alloc] init]; // NSLog (pGirl. showEnglishName); // NSLog (pGirl. showChineseName);} return 0 ;}
  
 


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.