The so-called compound, is the definition of the members of this class is another class of instance methods.
That is, the other objects as part of their own, to enhance their own functions,
is equivalent to a function nesting in the C language. Here is a piece of code (multiple files are put together):
1 /*Declaration of the **computer class ***computer declaration of Class ***computer * **/2 #import<Foundation/Foundation.h>3 4 @interfaceComputer:nsobject5 6@property (nonatomic, strong) NSString *brand;//declaring a String object property brand7 8 @end9 Ten /*Implementation of the **computer class ***computer class implementation of the ***computer class * **/ One #import "Computer.h" A - @implementationComputer - the @end - - /*Declaration of the **desk class ***desk declaration of Class ***desk * **/ - + #import<Foundation/Foundation.h> - + @interfaceDesk:nsobject A at@property (nonatomic, strong) NSString *color;//declares a String object property color - - @end - - /*Implementation of the **desk class ***desk class implementation of the ***desk class * **/ - in #import "Desk.h" - to @implementationDesk + - @end the * /*Declaration of the **classroom class ***classroom declaration of Class ***classroom * **/ $ Panax Notoginseng #import<Foundation/Foundation.h> - #import "Desk.h" the #import "Computer.h" + A @interfaceClassroom:nsobject the@property (nonatomic, strong) Desk *desk;//declares a desk object desk property Here's the compound . +@property (nonatomic, strong) computer *computer;////declares a computer object computer property Here's a compound . - @end $ $ /*Implementation of the **classroom class ***classroom class implementation of the ***classroom class * **/ - - #import "ClassRoom.h" the @implementationClassroom --(NSString *) description{//library methods, overriding of methodsWuyiNSString *str = [NSString stringWithFormat/*Method*/:@"Our classroom has%@ table,%@ computer", self/*the caller of the current method-classroom*/. Desk.color,self.computer.brand]; the returnstr; - } Wu @end - About /** * main function Main function * * * Main functions Main function * * * Main FUNCTION * * * Main function * * * $ - #import <Foundation/Foundation.h> - #import "ClassRoom.h" - A int main (int argc, const char * argv[]) { + @autoreleasepool { the - classroom *classroom = [[Classroom alloc] init]; $ Desk *de= [[Desk alloc] init]; the Classroom.desk = de;//assigns a value to the object's property (the object of the Class), first initializes the property the ClassRoom.desk.color = @ "Brown";//Assign a value to a property's property the the computer *com = [[Computer alloc] init]; - Com.brand = @ "Black Apple";//After the object is fully initialized, assign a value to the object to which it belongs in classroom.computer = com; the the NSLog (@ "%@", classroom); About } the return 0; the }
OBJECTIVE-C Composite