When writing object-oriented programs, we often want to add some new behavior to existing classes. Sometimes, we can create subclasses of this class. However, if we use the Toolset or the class library, we cannot handle the new subclass. At this point, Objective-c provides us with a very handy way- category .
So, what is a category? A category is a way to add a new method to an existing class. You just need to know the public interface of this class, and you don't have to know the source code of the class. The class definition can also be modularized to multiple related files.
The categories are also made up of declarations and implementations, and some syntax forms are declared as follows:
1 @interface already existing class name (category name) 2 // method Definition 3 ... 4 @end
The syntax format for the category Implementation section is as follows:
1 @implmentation A class name that already exists (category name) 2 // Implementing method Code 3 ... 4 @end
Category is a very important knowledge in objective-c, it usually has the following 3 kinds of usage:
1). Modular design of classes using categories;
2). Use categories to invoke private methods;
3). Use categories to implement informal agreements.
Below, let's show you how to use the category in this way, using an example. As far as programming habits are concerned, the general practice is to name the interface file name of the category "Class name + class name. h" In the form of implementing partial files named "Class name + class name. M".
First, we create a class file named: Nsnumber+calculate. Create a good class file that will automatically generate two files in your project:
Next, we'll write the interface file and the code that implements the file separately.
1 //2 //nsnumber+calculate.h3 //4 5 #import<Foundation/Foundation.h>6 7 @interfaceNSNumber (Calculate)8 //4 methods defined in a category9-(nsnumber*) Add: (Double) num2;Ten-(nsnumber*) Substract: (Double) num2; One-(nsnumber*) Multiply: (Double) num2; A-(nsnumber*) Divide: (Double) num2; - @end
Implement part of the code:
1 //2 //NSNUMBER+CALCULATE.M3 //4 5 #import "nsnumber+calculate.h"6 7 @implementationNSNumber (Calculate)8 //implementing the 4 methods defined by the interface section of a category9-(nsnumber*) Add: (Double) num2Ten { One return[NSNumber numberwithdouble: ([self doublevalue] +num2)]; A } --(nsnumber*) Substract: (Double) num2 - { the return[NSNumber numberwithdouble: ([self doublevalue]-num2)]; - } --(nsnumber*) Multiply: (Double) num2 - { + return[NSNumber numberwithdouble: ([self doublevalue] *num2)]; - } +-(nsnumber*) Divide: (Double) num2 A { at return[NSNumber numberwithdouble: ([self doublevalue]/num2)]; - } - @end
Finally, the definition method is called in main function to test whether the class method we define is implemented properly.
1 //2 //main.m3 //4 5 #import<Foundation/Foundation.h>6 #import "nsnumber+calculate.h"7 8 intMainintargcConst Char*argv[])9 {Ten One @autoreleasepool { Ansnumber* myNum = [NSNumber numberwithint:3]; - //Test the Add method -nsnumber* add = [myNum add:2.4]; theNSLog (@"ADD:%@", add); - //Test Substract Method -nsnumber* substract = [MyNum substract:2.4]; -NSLog (@"ADD:%@", substract); + //Test Multiply Method -nsnumber* multiply = [MyNum multiply:2.4]; +NSLog (@"ADD:%@", multiply); A //Test Divide Method atnsnumber* divide = [MyNum divide:2.4]; -NSLog (@"ADD:%@", divide); - } - return 0; -}
The results after normal operation are as follows:
Although a category can override methods in an existing class, it is generally not recommended. If you need to override the methods of the original class, the better advice is to implement the original method of the parent class through the derived class of the original class, and then the child class.
After you add a new method to a specified class by category, this new method affects not only the NSNumber class, but also all subclasses of the NSNumber class, each of which gets the method of the class extension. At the same time, you can define multiple categories for a class as needed, and different categories can add method definitions to the original class.
Well, the concept of category and the basic way to use it are introduced so much. In the next blog post, we'll cover several other important aspects of OBJECTIVE-C: protocols and delegates.
Objective-c Basic Tutorial Notes (11)--categories