"Objective-c Basic Course" Study notes (vi)--composite method

Source: Internet
Author: User

Today we are going to talk about the compound , of course, not a small couple arguing to break up, and then together in the compound.

  A composite follows a synthetic reuse principle , also known as a combination or aggregation multiplexing principle. The principle is to use object combinations as much as possible instead of inheritance to achieve reuse. Using aggregation can make the system more flexible, and the coupling between classes and classes is reduced. In Objective-c, compounding refers to the combination of multiple components, which are used together to obtain a complete work. Strictly speaking, only the combination between objects is compound. Basic data and structured objects in a class are not composited.

  inheritance and composition are two important relationships between classes and classes. Next, we'll borrow examples from the book to introduce the composite relationship .

If we were to build a simple car model, we would have an engine and four wheels (Tire). The code for the basic definition is as follows:

First, the definition and simple implementation of the tire class.

1 @interfaceTire:nsobject2 @end //Tire3 4 @implementationTire5-(NSString *) description{6     return(@"I am a tire. I last a while.");7 }8 @end

There is no definition of description in the implementation method, so where does he come from? In Cocoa, NSLog () can use the%@ format specifier to output an object, NSLog () sends the object a description message, and then the description method of the object generates a NSString and returns it. Therefore, you can customize NSLog () by providing the description method in the class.

Then define and simply implement the engine class.

1 @interface Engine:nsobject 2 @end 3 4 @implementation Engine 5 -(NSString *) Description {6     return @ "I am an engine . vroom! " ); 7 }8@end

Then define the car class itself.

1 @interfaceCar:nsobject2 {3Engine *engine;4Tire *tires[4];//Four wheels, defines an array of four numbers. 5 }6-(void) Drive;7 @end //Car8 9 @implementationCarTen-(ID) Init One { A     if(self =[Super init]) -     { -engine = [engineNew]; thetires[0] = [TireNew]; -tires[1] = [TireNew]; -tires[2] = [TireNew]; -tires[3] = [TireNew]; +     } -     return(self); + } A  at-(void) drive{ -NSLog (@"%@", engine); -NSLog (@"%@", tires[0]); -NSLog (@"%@", tires[1]); -NSLog (@"%@", tires[2]); -NSLog (@"%@", tires[3]); in } - @end

In the code that implements the car class, we define a method of init of type ID. The function here is to initialize the instance variable, create 1 engine variables and 4 tire variables. When you create a new object using new, the system automatically allocates memory to the object, and then automatically calls the Init method to put the object into a state that can be used.

About the Init method, if statement: if (self = [super init]), what does this condition mean? In order for the superclass (in this case: NSObject) to complete all the required initialization work at once. Need to call [Super Init]. The return value of the Init method (the ID type data, the generic object pointer) is the object being initialized. Assigning the returned result to self is a objective-c convention. This is done to prevent objects returned by the superclass from being inconsistent with the beginning of the creation during initialization.

Finally, in the main function, create a car object, and then call the drive method, the code is as follows:

1 int Main (intconstChar * argv[])2{3     Car *car; 4     New ]; 5     [car drive]; 6     return 0 ; 7 }

Operating effect:

"Objective-c Basic Course" Study notes (vi)--composite method

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.