1. classification: when we want to add some methods to a class, if we don't want to inherit the class, we can add some behavior to the class by classification. This process is lighter than inheritance.
@ Interface nsstring (subclass)-(ID) dosomething (); @ end @ implementation nsstring (subclass)-(ID) dosomething () {// do something...} @ end
In the code above, the first line, subclass is the category name, And nsstring is the class for adding behavior. The implementation of classification is similar to that of general classes.
2. Protocol: the protocol is a set of predefined behavior methods, which is similar to interfaces in Java. The protocol can be divided into formal and informal protocols. The former is the method that must be defined by the class using the protocol, and the latter is optional.
@ Protocol handleevent {-(void) handlekeyupevent;-(void) handlekeydownevent: (ID) sender ;}
3. attribute: the attribute is used to access the instance variables of the object. The usage is to use @ property in the header file (. h) to declare an attribute, and then use @ synthesize in the implementation file (. m) to tell the compiler to automatically generate an access method for a variable.
@ Property (nonatomic, retain) nsstring * Name; @ synthesize name;
Classification/protocol/attribute