Oc Lesson 3, oc
Main Content: inheritance, initialization method (key), convenience Constructor (key)
1. Inheritance
Inheritance means that the subclass inherits all instance variables and methods of the parent class. The parent class has some, but the subclass has some, and the parent class does not necessarily have
Upper-level inheritance: parent class, lower-level inheritance: subclass
Inheritance is a single direction and cannot inherit from each other
Inheritance is passed: A inherits from B, B inherits from C, and A has all the features and behaviors of B and C.
Write the common methods and instance variables to the parent class. The subclass only needs to write its own unique instance variables and methods, which greatly simplifies the code.
Inheritance not only ensures the integrity of the class, but also simplifies the code.
:
For inherited implementations
Inheritance features:
1. In OC, only single inheritance is allowed
2. A class without a parent class is called the root class, and the root class in OC is NSObject (ancestor)
3. Inheritance Method: all are inherited
4. If the subclass is not satisfied with the method of the parent class, you can overwrite the method of the parent class.
Execution of the inherited method:
Super is a compiler instruction, not an object
Purpose: Send a message to super and execute the methods in the parent class.
Note: The subclass overrides the parent class method. The subclass has both its own implementation and the implementation inherited by the parent class. To use the implementation of the parent class, send a message to super.
Ii. Initialization Method
Create an object in two steps: Open up space and initialize
Main Content: assigning initial values to some instance variables
The initialization method is only used once in the entire lifecycle of the object.
Complete initialization method example:
-(Instancetype) init
{
// Use the method inherited from the parent class first
Self = [super init];
// Determine whether the initial score is successful
If (self ){
// Initialization settings
}
// Return
Return self;
}
Before learning inheritance, there is no self = [super init]. We assign values to instance variables one by one.
After learning inheritance, the parent class declares the public instance variables. As the parent class, it should also have its own initialization method to assign initial values to these public instance variables.
Initialization Process:
1. In your own initialization method, the initialization method of the parent class is preferentially called,
2. The Initialization Method of the parent class calls the initialization method of the parent class in turn and calls it up
3. After the initialization method at the top layer is complete, return to the initialization method at the second layer to complete the initialization method at the second layer.
4. Return to the initialization method on the third layer until the initialization method of this class is complete.
Features of the initialization method:
1. The initialization method starts "-".
2. return values of the id or instancetype type
3. Start with init
Internal implementation: first execute the super initialization method, while executing its own variable, return self;
Specify the initialization method:
The so-called specified initialization method: is a class of many initialization methods, no matter which initialization method is used, there is always an initialization method called, this initialization method is called the specified Initialization Method
1. A class can have many initialization methods.
2. There is usually a specified Initialization Method
3. the initialization method with the most parameters is usually used as the specified initialization method.
4. Each class has a specified initialization method. If the class has only one initialization method, the initialization method is to specify the initialization method.
5. Specify the initialization method to send messages to super
Iii. Constructor
The constructor makes a step forward based on the initialization method, encapsulating the object creation process.
The constructor is a "+" method that returns an instance of this type. The method name starts with a class name.
Note: The internal implementation encapsulates alloc and initialization methods, making it easier to use.