IOS development-OC language (7) Inheritance, polymorphism, category, ios-oc
Inheritance, polymorphism, and category
Learning Objectives
1. Meaning of Inheritance
2. Alias of the parent class subclass
3. Inheritance of fields and messages
4. Message rewriting and rewriting calls
5. Polymorphism
6. Category)
========================================================== =====
1. knowledge to be understood
Three basic features of object-oriented: 1. encapsulation 2. Inheritance 3. Polymorphism
1. Inheritance (single inheritance): Relationship between parent class (superclass) and subclass.
The two inherited classes have a parent-child relationship.
OC supports single inheritanceMulti-inheritance is not supported.
1.1 inheritance in life
People (parent class) -- men/women (Child class)
Men/women (parent class) --- students (Child class)
Student (parent class) --- College Student (Child class)
College student (parent class) --- senior student (Child class)
Senior (parent) --- James(Subclass)
[Note]
(1) inheritance is also called derivation.
The attributes (member variables (excluding private) and methods of the parent class can be directly obtained by subclass. This process is called inheritance.
Child classes create their own member variables and methods based on the parent class. This process is called derivation.
Inheritance and derivation are different focuses of the concept of inheritance.
(2) tip: each class has a super class pointer pointing to its parent class. The object has an isa pointer pointing to the class that calls the object.
Benefits of inheritance:
1) when creating a large number of similar classes, it can save the workload. Extracted repeated code
2) use the class in the framework or the class that has been written to inherit the class and generate a derived class, which is easier to use than the original class.
Note: NSString, NSArray, and NSDictionary cannot be inherited.
3) establish a connection between classes
Disadvantages of inheritance:
Strong Coupling
Reason why NSString cannot be inherited:
The NSString class adopts the "Abstract Factory" mode during design. It contains a class cluster and a class cluster. That is to say, NSString is a "factory class", and then it provides many method interfaces in the outer layer, but the implementation of these methods is implemented by specific internal classes. When an object is generated using NSString, the initialization method determines which "internal class" is the most suitable for generating this object, then the "Factory" will generate this specific Class Object and return it to you. This type of outer class provides a unified abstract interface, and then the specific implementation is hidden, and the specific internal class is implemented.
1.2 inheritance in code
<1> basic inherited syntax
(1) @ interface subclass name: parent class name
@ End
(2) After inheritance
Subclass can use all common methods of the parent class
Subclass to obtain non-private member variables of the parent class
<2> rewrite
1. Subclass method inherited from the parent class. Sometimes it is not suitable for subclasses. Subclass can override this method.
2. Rewrite is to re-implement the method of the parent class (the method name is the same and the implementation content is different)
3. After the subclass overrides the parent class method, the subclass object will eventually execute the method after the subclass is overwritten.
1.3 embodiment of inheritance in memory
The Child class inherits the parent class. Composition of subclass objects:
1) fields and methods of the parent class
2) unique fields and methods of sub-classes
[Role of inheritance]
1. quickly create multiple similar classes
2. Extend the class
3. Customize some system functional controls (many functions are used in the UI)
[Rewrite] methods inherited from the parent class are not suitable for subclass. Subclass can override this method.
[Rewriting is a type of polymorphism, and polymorphism is the same declaration of different implementations]
[Note] in many cases, the subclass method only needs to add some content to the method with the same name as the parent class. You can use the keyword supper to call the method of the parent class, and then add the unique content of the subclass.
1.4KeywordsSuper
Super keyword. When you override a method in a subclass, the caller can skip this layer and call the method in the parent class.
Purpose:
(1) directly call a method in the parent class
(2) If Super is in the object method, the object method of the parent class will be called; if super is in the class method, the class method of the parent class will be called.
Usage scenario: When a subclass overrides the parent class method, it wants to retain some behavior of the parent class.
1.5Inheritance and combination
Applicable scenarios of inheritance:
(1) When two classes have the same attributes and methods, the same attributes and methods can be extracted to a parent class.
(2) When Class A has partial attributes and methods in Class B, Class B can be considered to inherit Class A (consideration). In this case, you can also consider using combinations.
Inheritance: ### it is xxx. For example, if a dog is an animal, the dog can inherit the animal class.
Combination:###YesXxxIf students have books, they can use books as students.Class attributes
-------------------
2. Message Mechanism of polymorphism and Classes
[Polymorphism] rewriting and virtual functions
// In fact, polymorphism means that different implementations of the same interface
Different implementations of the same method are called polymorphism. Rewriting is a type of polymorphism.
Method Overloading is also a type of polymorphism. Method overloading refers to defining multiple methods with the same name in a class, but each method must have different parameter types or number of parameters.
[Note] OC does not have strict method overloading.
[Virtual method]
Virtual Function Definition: when calling a method, you can call a virtual method without looking at the pointer, the object, and the object address.
Advantages of Virtual Methods: It can describe that different things are triggered by the same event to produce different responses (results)
1. Response Message. You can view the object without looking at the pointer.
2. the pointer to the parent class, which can point to the object of the subclass (the object of the subclass is assigned to the parent class ).
[Note]
The virtual method indicates that different things are triggered by the same event and have different responses.
Send the same message to different objects, and the response is different.
Implement polymorphism (meeting three conditions)
1. There is an inheritance relationship
2. Rewrite
3. the pointer of the parent class points to the object of the subclass.
[Note] NSString NSArray NSDictionary NSNumber is both a [factory class] or an abstract class and cannot be inherited. Even if it is inherited, it cannot use the parent class method.
-------------------
5. category/category
CATEGORY statement:
@ Interface Class Name (category name)
Definition Method
@ End
How to provide a class extension method?
1. Inheritance
2. Category
Note: Once a class is used to add methods to an existing class, the class object can use this method.
<2>. Category function:
1. You can add methods to existing/system native classes, but cannot add global variables and attributes.
2. You can manage classes by category.Class implementation is dispersed into multiple different files or multiple different frameworks.
<3> usage categories
1. member variables cannot be added to a category.
2. To add a category, you must import the category header file.
3. Methods in the parent class, subclass can also be used
<4> class Extension
If you do not want to expose some class methods, you can use class extension.
1. Basic Syntax of class Extension
In the. m file
@ Interface Person () // No Name
-(Void) song;
@ End
@ Implementation Person
-(Void) song {
}
@ End
Note: The declarations and implementations of class extensions are stored in the. m file.
2. Class extension Functions
1. Private methods can be implemented
2. The member variables can be declared. The member variables are private and cannot be accessed by sub-classes or other classes.