If (Self = [Super init] {…
To perform one-time initialization for a superclass, call [Super init]. The value returned by the init method (ID data, that is, generic object pointer) describes the initialized object.
Assigning the result of [Super init] to self is a standard practice of objective-C. This is done to prevent the objects returned by the superclass during initialization from being different from the previously created objects.
-(ID) Init // initialization object {If (Self = [Super init]) {// initialization content} return self ;}
Access Method
An accessor method is a method used to read or change the specific attributes of an object.
Setter method: mutator is used to change the object state.
Getter method: The getter method provides a way to read object attributes by using the object code.
Note: When operating on attributes of other objects, you should always use the access method provided by the object and never directly change the values of other object attributes. For example, main () should not directly change the value of the engine attribute through car-> engine, but should be changed using the setter method.
Naming rules: The setter method is named based on the name of the property it has changed, and the prefix "set" is added. The getter method is only named based on the name of the property it returns (without the get prefix ).
# Import <Foundation/Foundation. h>/** car3: Add a new car engine V8 and weatherradial tires **/@ Interface Engine: nsobject @ end @ implementation engine-(nsstring *) description {return (@ "I Am a engine") ;}@ end @ interface tire: nsobject @ end @ implementation tire-(nsstring *) description {return (@ "I Am a tire");} @ end # pragma mark *** car *** @ interface car: nsobject {Engine * engine; tire * tires [4];} // Add getter, setter-(engin E *) engine;-(void) setengine :( engine *) m_engine;-(tire *) tireatindex: (INT) index; // access this property through the indexer-(void) settire: (tire *) m_tire: (INT) index;-(void) print; @ end @ implementation car //-(ID) init // initialize car // {// If (Self = [Super init]) {// engine = [engine new]; /// tires [0] = [Tire new]; // tires [1] = [Tire new]; // tires [2] = [Tire new]; // tires [3] = [Tire new]; //} // return self; //}-(Engine *) engin E {return engine;}-(void) setengine :( engine *) m_engine {Engine = m_engine;}-(tire *) tireatindex :( INT) index {If (index <0 | index> 3) {nslog (@ "Bad index (% d) in \" tireatindex: \ "", index ); exit (1);} return tires [Index];}-(void) settire :( tire *) m_tire :( INT) index {If (index <0 | index> 3) {nslog (@ "Bad index (% d) in \" settire: atindex \ "", index); exit (1) ;}tires [Index] = m_tire ;} -(void) print {nslog (@ "% @ ", Engine); nslog (@" % @ ", tires [0]); nslog (@" % @ ", tires [1]); nslog (@ "% @", tires [2]); nslog (@ "% @", tires [3]);} // if the description method is not rewritten, then % @ will output the object's memory address. // For example: <car: 0x10010c480>-(nsstring *) Description {return @ "I am a car! ";}@ End // ********************* // V8 engine class @ interface V8: engine @ end @ implementation V8-(nsstring *) Description {return @ "I Am a V8 engine! ";}@ End // V8 // ********************** // weatherradial tire @ interface weatherradial: tire @ end @ implementation weatherradial-(nsstring *) Description {return (@ "I Am a weatherradial tire !! ") ;}@ Endint main (INT argc, const char * argv []) {car * car; car = [Car new]; V8 * Engine = [V8 new]; [Car setengine: Engine]; int I; for (I = 0; I <4; I ++) {weatherradial * tire = [weatherradial new]; [Car settire: Tire: i];} [Car print]; return 0 ;}
Combination or inheritance?
Inheritance establishes the "is a" relationship between objects. If we can say that X is a Y, we can use inheritance.
Composite creates a "has a" relationship. If you can say that X has a Y, you can use composite.