1. Category
A category is used to add a new method for an existing class. However, you cannot add instance variables. For example, the system class cannot see its. M file, so there is no way to directly add the method to implement it.
@ Interface nsmutablearray (SORT) // Add the sort method to the nsmutablearray class. sort is the category name, so that you can understand the name.
-(Void) invert; // Method
@ End
Implementation
# Import "nsmutablearray + sort. H"
@ Implementation nsmutablearray (SORT)
-(Void) Invert
{
Nsuinteger COUNT = self. count;
For (INT I = 0; I <count/2; I ++)
{
[Self exchangeobjectatindex: I withobjectatindex: count-i-1]; // The first and last objects are exchanged
}
}
@ End
In the main function, you can directly call the invert method above.
Nsmutablearray * arr = [[nsmutablearray alloc] initwithobjects: @ "12", @ "4", @ "89", @ "88", @ "36", nil];
[Arr invert];
Nslog (@ "% @", arr );
Print result:
11:12:03. 324 category [980: 14503] (
36,
88,
89,
4,
12
)
What are the application scenarios of category:
1. The class contains many methods to implement, and these methods need to be implemented by members of different teams.
2. When you are using the classes in the base class library, you do not want to inherit these classes but just want to add some methods.
Category can meet the above requirements. Of course, you need to pay attention to the following issues when using category:
1. Category can access the instance variables of the original class, but cannot add instance variables. If you want to add variables, You can inherit to create subclass.
2. Category can reload the methods of the original class. It is not recommended to do so. This will overwrite the methods of the original class. If you do need to overload it, you can create a subclass by inheriting it.
3. What is different from a common interface is that the instance method in the category implementation file does not need to implement all declared methods as long as you do not call it.
2. Extend
The function of extension is to define your own private method.
The format is the same as the category. You do not need to create a new file. Instead, you only need to place the file in the interface of the class to the. M file of the class you want to expand.
# Import "student. H"
@ Interface student (Extension) // create a private text method for the student class
-(Void) text;
@ End
@ Implementation student
-(Void) hello;
{
[Self text];
}
-(Void) Text
{
Nslog (@ "hello ");
}
@ End
When the external [STUDENT Hello] is displayed, "hello" is displayed ". However, the text method cannot be called outside the world. [STUDENT text] is incorrect. You can only use self in the. M file to call the private method text. The internal implementation is hidden.
3. Protocol
(1). The Protocol declares methods that can be implemented by other classes. The Protocol itself will not be implemented.
(2). The Protocol is not a class, but an interface that can be implemented by other objects.
Define a protocol:
# Import <Foundation/Foundation. h>
@ Protocol marryprotocol <nsobject> // defines a wedding agreement, which can inherit other protocols.
@ Required // The required protocol. Implement the method in the. M file of the class that complies with the marryprotocol protocol.
-(Void) washclothes;
-(Void) cook;
@ Optional // Optional Protocol
-(Void) makemoney;
@ End
Proxy (Delegation) mode: You have your own needs, but let others do it.
Define a Men class
# Import <Foundation/Foundation. h>
# Import "roomservice. H"
@ Interface men: nsobject
@ Property (nonatomic, retain) nsstring * Name;
@ Property (nonatomic, assign) ID <marryprotocol> mate; // defines a proxy.
-(ID) initwithname :( nsstring *) Name;
-(Void) needeat;
@ End
Define a women class
# Import <Foundation/Foundation. h>
# Import "marryprotocol"
@ Interfacewomen: nsobject <marryprotocol> // complies with the protocol.
@ Property (nonatomic, retain) nsstring * Name;
-(ID) initwithname :( nsstring *) Name;
@ End
In the implementation of men
-(Void) needeat
{
[Self. mate cook];
}
There is a method in the implementation of women.
-(Void) Cook
{
Nslog (% @ "wonmen cooking ");
}
Inside the main function
Men. Mate = women;
[Men needeat];
Print out: wonmen is cooking
4. The protocol can also be written in a class. When used, you can introduce the Class header file.
Category, extension, Protocol