OC basics: inheritance. Initialization Method, convenient Constructor
Inheritance:
1. Unidirectional inheritance. A class can have only one parent class, and a parent class can have multiple child classes.
2. Unidirectional inheritance. The base class (root class) is OSObject.
3. Subclass can inherit the attributes and methods of the parent class
When the method of the parent class does not meet the requirements of the subclass, The subclass can override the method of the parent class, override the method of the parent class, and do not need to declare it again in the subclass.
1. completely rewrite
2. Partially rewrite using super
After an inheritance relationship is established, the Child class can inherit from the parent class:
1. instance variable, @ public @ protected
2. Public Methods
If a method is declared in. h, this method is a public method. If it is not declared, it is private.
There is no absolute private method in OC.
Process of sub-class calling method:
First, search for methods in this class. If the methods are found, call them directly. If the methods are not found, search for them in the parent class. If the methods are not found in the parent class, the next step is to look up (the parent class of the parent class) until it is found.
If it cannot be found, an exception is thrown, causing crash.
Objects can be assigned values. objects are pointers.
The custom initialization method does not need to be declared in the. h file because it is overwritten.
The traversal constructor must be a class method.
Return Value Type (id, instancetype)
Custom initialization method: initialization + assignment.
Convenience constructor: Open up space + initialization + assignment.
Custom initialization method:
-(Id) initWithColor :( NSString *) color
Price :( NSString *) price {
If (self = [super init]) {
_ Price = price;
_ Color = color;
}
Return self;
}
Constructor:
+ (Id) appleWithColor :( NSString *) color
Price :( NSString *) price {
Apple * apple = [[Apple alloc] initWithColor: color price: price];
Return apple;
}