The next thing I'm going to talk about is compounding.
Learning with questions will be quicker
What is a compound? What are the advantages? When do you use the compound? What is a dependency relationship? How to solve?
Compounding is the combination of individual components to form a whole. (One class defines another class as an instance variable)
Advantages:
1. Refer other objects to the newly created object, forming a new whole and more powerful.
2. Less coupling (relative to inheritance)
Inheritance is the "is a" is a, composite is "have a" has a, such as a computer has a keyboard, the car has an engine and so on can be used to compound
Dependency: A relationship between a class and another class or multiple classes
For example, A.h file import B.h file, B.h file import C.h file, so three files to establish an association
If a new instance variable is added to the C.h file, then the B.h,a.h file will be recompiled to accept the change, which is the dependency
With @class instead of #import, the @class is to tell the compiler that this is a class that can be used with ease, and the compiler doesn't need to know what's inside the class, but just know it's a class that can be referenced by pointers
Let's use an example to understand how the composite is used
Lift a car, car, engine, Tyre (Tire), use a composite to complete the assembly of a car
Create a project, the main function does not move it, build the engine class
The. h file does not move it
Creating the Tire Class
Just like the engine class.
Create a Car class
Here is used to @class, the engine class and tire class as an instance variable, do not call the contents of the class can be used @class otherwise use #import
We'll assemble the car here.
Instantiation in CAR.M file
Implement the output in the main function
This is only a very simple composite example, if you want to go into the reader can write their own, such as the car's four tires have their own different positions, when visiting a car on a tyre to specify the exact location of the tyre, so in car also rewrite Settire: (tire*) Newtire Andindex: (int) Index setter method, also add indexed getter method, need to pay attention to add if judge the range of tyre value.
OBJECTIVE-C Composite