OBJECTIVE-C Composite

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.