Inheritance, initialization method
First, inheritance
Upper layer of Inheritance: parent class inherits from lower layer: subclass
Inheritance is one-way and cannot inherit from each other
Inheritance has transitivity: a inherits from B,b inherited from C,a features and behaviors that have B and C
Subclasses can inherit all the characteristics and behaviors of the parent class (the private variable also inherits, but cannot be accessed)
Object-oriented provides inheritance syntax. Can greatly simplify the code, the public method and the instance object is written in the parent class. Subclasses only need to write their own unique instance variables and methods to
Inheritance guarantees both the integrity of the class and the ability to simplify the code
Inheritance Features
Only single inheritance is allowed in OC
A class without a parent class is called the root class, and the root class in OC is NSObject (ancestor)
Inherited content: All instance variables and methods
If the subclass is not satisfied with the implementation of the parent class method, you can override the parent class's method
Inheritance tree
Execution of methods in inheritance
Super keyword
Super is a compiler directive, and an object.
Role: To send Super message, can hold? Method that is implemented in a class.
The class can override the. class, that is, the class has its own implementation, the implementation of the parent class inherited, if you want to make the. With the implementation of the parent class, send a message to super
// override Sayhi Method -(void) sayhi{ [Super Sayhi]; // calling the Parent class Sayhi method NSLog (@ "OK");}
-(void) study// learning { [super Sayhi]; // calling the Parent class Sayhi method [Self sayhi]; // call itself Sayhi method NSLog (@ "%@ I'm studying hard ", _name);}
Second, the method of initialization
Create objects in two steps: Open space, initialize.
The primary use of the Initialize method is to assign an initial value to some instance variable.
Initialize the method in the whole life cycle of an object?
-(Instancetype) Initwithname: (NSString *) name sex: (NSString *) Sex number: (nsinteger) number; // instancetype Replacement ID-(ID) Initwithname: (NSString *) name sex: (NSString *) Sex number: (nsinteger) number;
Initializes the initialization method of the parent class, initializes the variables in the parent class, and initializes its own variables.
Each class is aware of its own unique variables, so. The initialization method simply initializes its own unique variable, initializing the inherited variable by invoking the parent class's initialization method.
After a call, all the variables on the inheritance tree can be initialized
The least-scientific initialization method: In a subclass, assign a value to each variable, because you need to look up which variables you have inherited.
The class defines an instance variable other than the public instance variable in the class. In its own initialization method, priority is given to super to send the INIT message, initialize the public variables, initialize the success, and then initialize the body-specific variables, from the? Complete initialization of all instance variables.
Initialization process
1. What is the implementation of the initialization method in the parent class? (recursion up to the NSObject class? method)
2. Start the line initialization method from the root class NSObject.
3. Determine if the initialization in the parent class is successful, that is, whether self exists.
4. Complete the initialization settings of the object and return the object.
5. Initialize the sub-class instance variable.
Initialize Method features
Initialize? method is "-"? method
The return value of the ID or instancetype type
Start with Init
Can be implemented internally with 0 to multiple parameters: first, the initialization of the line super? method, then initialize the body variable, return to self.
Third, the convenience of the construction device
The convenience constructor advances on the basis of the initialization method? Small steps.
Encapsulates the object creation process.
The convenience constructor is the "+" method, which returns an instance of this type, Dharma name begins with the class name.
There can be 0 or more parameters. Internal implementation: Encapsulates the Alloc and initialization method. Make it more concise to use.
Implementing code in a function
+ (Student *) Studentwithname: (NSString *) name sex: (NSString *) Sex hobby: (NSString *) Hobby Age: (Nsinteger) Age number :(Nsinteger) number{ return [[Student alloc] initwithname:name sex:sex hobby:hobby age:age Number:number];}
Inside the main function code *student1 = [Student studentwithname: @ "wangw" sex:@ "man " hobby:@"xuexi" Age: number: [];