In C ++, we can implement more inheritance.CodeReuse and EncapsulationProgramMore concise. In objective-C, only single inheritance is allowed, and no multi-inheritance is allowed. In addition to the Protocol protocol, we can implement a method similar to multi-inheritance, that is, classification (also called category ). You can add a new method to the original class without modifying the original class (parent class) or the derived class, but the instance variable cannot be added to the category.
Format ):
@ Interface class_name (category_name) <protocol,...>
Method _ Declaration;
....
@ End
@ Implementation class_name (category_name)
Method_implementation;
...
@ End
Create an addresscard class, and then create a new file addresscardcategory to declare and implement the category)
Addresscard. hView
Code
Addresscard. m
View code
Addresscard (Category) classification declaration File
View code
Addresscard (Category) implementation file
View code Main
Function: view code
Execution result:
17:12:04. 751 addresscard [1079: 707] Card 4 Shou, abc@126.com
17:12:04. 758 addresscard [1079: 707] Card 4 Shou, abc@126.com