I. Inheritance
1. Inherited upper layer: parent class, inherited lower layer: subclass
2. Inheritance is one-way
3. Inheritance has transitivity: subclasses inherit the characteristics and behavior of the parent class
4. Subclass extension Parent class, more specific
Inheritance in OC
Inheritance in 1.oc, where one class inherits from another class;
2. The inherited class is called the parent class or superclass;
3. Inherited classes are subclasses
Characteristics of inheritance
Only single inheritance allowed in 1.oc
2. A class without a parent class is called the root class, and the root class in OC is NSObject
3. Inherited content: All instance variables and methods except private variables
4. Subclasses can override methods of the parent class
Super
Keywords in 1.oc
2. Role: Send a message to super, executable method implemented in the parent class
Two. Initialization method
-(ID) init
{
Send the INIT message to super; Execute the Init method implemented in the parent class
self = [super init];
if (self) {
Initialize settings
}
Returns the initialization completion object
return self;
}
Initialization process
1. Execute the method implemented in the parent class (recursively up to the initialization method in the NSObject class);
2. Start the initialization method from the root class nsobject;
3. Determine whether the initialization in the parent class is successful, that is, whether self exists
4. Complete the initialization settings of the object, return the object
Three. The construction of the toilet
1. Declaration and implementation of the convenience builder
+ (ID) personwithname: (NSString *) name
{
Person *p =[[person alloc]initwithname:name];
return p;
}
2. Creating objects with the convenience builder
Person *p =[Person personwithname:@ "Niaho"];
Objective-c (inheritance, initialization method)