IOS classification ideas (1)

Source: Internet
Author: User

1. requirement: If you want to expand an additional method for a class without changing the code, you can use inheritance and Classification 2. category: You can expand some object methods or class methods for a class without modifying the original class code. Therefore, a class can have multiple categories: category is also a class, so it is also clear and implemented. Classification is generally implemented by using the function or module name Declaration: @ interface Class Name (category name) @ end implementation: @ implementation Class Name (category name) @ end3. note (1) only methods can be added for classification, but member variables cannot be added. If you want to add member variables, use inheritance for implementation. (2) in the classification implementation method, the member variables in the classification class can be merged, but the attribute @ property (3) in the classification class can be re-implemented in the original class, however, it will overwrite the methods in the original class and will become invalid. This is because when executing the object member method, it will first search for the class, then go to the original class to find it, and finally go to the parent class to find it. But it is generally not recommended! (4) If a class has multiple categories and there are methods with the same name in the category, the final compiled category will overwrite the previously compiled category and output 4. conclusion: Based on the classification idea, if a class has many methods to implement functions, you can separate each function method into a category so that specific categories can be used to manage specific functions! In this way, if other classes need to implement the same function, you can directly call the category. In actual development, it is generally not recommended to write the category for custom classes, generally, it is an example of the built-in class write classification: the Peron class has two categories: Person + QM and Person + MM. Copy the code/*********************** Person. h file **********************************/# import <foundation/Foundation. h> @ interface Person: NSObject {int _ personCount;} @ property int personCount;-(void) test; @ end copy code/************************ Person. m file **********************************/# import" person. h "@ implementation Person-(void) test {NSLog (@" execute the test method in the original class Person ");} @ end copy code/************************ Person + QM. h file **********************************/# import" person. h "@ interface Person (QQ)-(void) study;-(void) test; @ end copy code/************************ Person + QM. m file **********************************/# import" person + QM. h "@ implementation Person (QQ)-(void) study {NSLog (@" ");} // overwrite the test of the original Person-(void) test {NSLog (@ "executing classification Person + test method in QM ");} @ end copy code/************************ Person + MM. h file **********************************/# import" person. h "@ interface Person (MM)-(void) test; @ end copy code/************************ Person + MM. m file **********************************/# import" person + MM. h "@ implementation Person (MM)-(void) test {NSLog (@" execute classification Person + MM. the test method in h ");} @ end copy the code to copy/************************* main. m file **********************************/# import <foundation/Foundation. h> # import "Person + QM. h "# import" Person. h "int main (int argc, const char * argv []) {Person * person = [[Person alloc] init]; [person test]; [person study]; return 0;} copy the code output: 14:07:04. 281 classification [898: 303] perform the test method in the classification Person + QM 14:07:04. 282 classification [898: 303] Learn why the methods in the Person + QM classification are output. This is related to the compilation sequence.

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.