First, the basic concept
Program of the world and the Human "object" of the world in the mind is no difference, rich two generations inherit the parents, naturally have all the resources parents have, the subclass inherits the parent class also has the parent class all methods and attributes (member variables).
Here animals are the parents of cats and dogs, and black and white cats are subcategories of cat classes.
Benefits of Inheritance:
(1) The duplicated code is extracted.
(2) establishing a link between classes and classes
Disadvantages of Inheritance:
Coupling is too strong
Second, the inheritance in OC
@interface Animal:nsobject
The animal inherits the NSObject, obtains the NSObject class the method;
@end
@interface Dog:animal
Dog class inherits Animal class
@end
Note: The OC language is a single-inheritance language. In the OC language, basically the root class of all classes is the NSObject class.
Third, the use of inheritance note
(1) The compiler executes from top to bottom, so there should be at least a declaration of the parent class in front of the subclass;
(2) The name of a member variable with the same name is not allowed in OC and the parent class;
(3) The subclass in OC can have the same name as the parent class method, in the subclass call, take precedence of their own internal search, if not the first layer of the upward looking;
Tip: The subclass overrides a method in the parent class, overwriting the previous implementation of the parent class.
: There are three classes, the person class inherits from the NSObject class, and the student class inherits the person class.
Create a Student *s=[[student alloc] init];
The student class and the parent class of this class are loaded into memory at this point.
Tip: There is a super class pointer in each class that points to your parent class. There is an Isa pointer in the object that points to the class that called the object.
Iv. Inheritance and composition
Application of Inheritance:
(1) When two classes have the same properties and methods, the same properties and methods can be extracted into a parent class.
(2) When Class A fully possesses some of the properties and methods in Class B, it may be considered that Class B inherits Class A (consider), in which case a combination can also be considered.
Inheritance: # # #是xxx, such as dogs are animals, can let dogs inherit animal class
Combination: # # #拥有xxx, if the student has a book, you can make the book this class as a property of the student class
Five, the key word super
The Super keyword, when overriding a method in a subclass, allows the caller to skip this layer and invoke a method in the parent class.
Role:
(1) Call one of the methods in the parent class directly
(2) Super is in the object method, then the object method of the parent class is called, and super is in the class method, then the class method of the parent class is called.
Usage Scenario: Subclasses want to preserve some behavior of the parent class when overriding the parent class method.
OC Object-oriented-inheritance