22-Dark Horse programmer------OC Language Learning Notes---categories

Source: Internet
Author: User

Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
1 classification
The syntax format for the classification is:
1, the classification of the Declaration:
@interface class names (category name)
Method declaration
@end
2, the realization of the classification:
@implementation class names (category name)
Method implementation
@end
In the declaration section, the classification can only increase the method and cannot increase the member variable; the member variables declared in the main class can be accessed in the classification method implementation

The function of classification: You can add some methods to the class without changing the contents of the original class.

Use Note:

1. classification can only increase the method, cannot increase the member variable

2. member variables declared in the original class can be accessed in the classification method implementation

3. classification can re-implement the methods in the original class, but will overwrite the original method, will cause the original method can not be used

4. priority of method invocation: Classification ( the last category that participates in compilation takes precedence)--and the original class---the parent class

Person.h  @interface person:nsobject  {      int _age;  }  @property int age;  -(void) test;  @end    //person.m  @implementation person  -(void) test  {      NSLog (@ "person-test");  }  @end    Classification of//PERSN, person+jj.h  @interface person (JJ)  -(void) test2;  @end    //PERSN Classification, person+jj.m  @implementation person (JJ)  -(void) test2//Implement the method declared in the taxonomy  {       _age = 10; Access the member variable NSLog of the main class       (@ "-----test2,age=%d", _age);  }  -(void) test//To re-implement the test method in the main class, overwriting the test method in the main class, causing the method of the main class to be unable to use  {       NSLog (@ "person (JJ)-test");  }  @end  

  

2 Application of classification
Application of classification, add a class method to NSString: Calculates the number of Arabic numerals in a string; Adds an object method to NSString: Calculates the number of Arabic numerals in the current string

Nsstring+number.h  @interface nsstring (number)    + (int) numbercountofstring: (NSString *) str;    -(int) numbercount;    @end    //nsstring+number.m  @implementation nsstring (number)      + (int) numbercountofstring: (NSString *) str  {              return [str numbercount];  }  -(int) numbercount  {      int count = 0;      Unichar = c = [self characteratindex:i];      for (int i = 0; i<self length= "" i= "" if= "" c= "" >= ' 0 ' && c<= ' 9 ')          {              count++;          }      }            return count;  }    MAIN.C  int main ()  //Class Library: Collection of many classes  {      int count1 = [NSString numbercountofstring:@] 54D4DF3A43SDF43DASD "];            int count2 = [@ "9fdsdffds54df3543" numbercount];            NSLog (@ "%d", count1);      NSLog (@ "%d", count2);      return 0;  }    @end  </self>  

  

22-Dark Horse programmer------OC Language Learning Notes---categories

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.