The three basic features of object-oriented are encapsulation, inheritance, and polymorphism.
Encapsulation
Encapsulation is better understood. Encapsulation is one of the characteristics of object-oriented, and is the main feature of object and class concepts.
Encapsulation refers to encapsulating objective objects into abstract classes. classes can only perform trusted class or object operations on their own data and methods to hide untrusted information.
Inheritance
One of the main functions of Object-Oriented Programming (OOP) is "inheritance ". Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class.
The new class created by inheritance is called a subclass or a derived class ".
The inherited class is called "base class", "parent class", or "super class ".
The process of inheritance is from general to special.
To implement inheritance, You can implement it through "inheritance" and "Composition.
In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can only have one base class. To implement multi-inheritance, You can implement multi-level inheritance.
Inheritance concepts can be implemented in three ways: Implementation inheritance, interface inheritance, and visual inheritance.
ØImplementation Inheritance refers to the ability to use the attributes and methods of the base class without additional encoding;
ØInterface inheritance only uses the names of attributes and methods, but the subclass must provide implementation capabilities;
ØVisual Inheritance refers to the appearance and implementation of a child form (class) using a base form (class ).CodeCapabilities.
When considering inheritance, note that the relationship between the two classes should be "Belong. For example, if the employee is a person and the manager is a person, both classes can inherit the person class. However, the LEG class cannot inherit the person class, because the leg is not a person.
An abstract class only defines the general attributes and methods that will be created by the subclass. When creating an abstract class, use the keyword interface instead of class.
The OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (Inheritance and synthesis) → designing and implementing classes and instances.
Polymorphism
Polymorphisn is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class.
There are two methods to achieve polymorphism: overwrite and reload.
Overwrite refers to the method by which the subclass redefines the virtual function of the parent class.
Overload means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both are different ).
In fact, the concept of overloading is not "Object-Oriented Programming". The implementation of overloading is that the compiler modifies the name of a function with the same name based on different parameter tables of the function, then these functions with the same name become different functions (at least for the compiler ). For example, there are two functions with the same name: function func (P: integer): integer; and function func (P: string): integer ;. The modified function names of the compiler may be: int_func and str_func. For the call of these two functions, it has been determined between the compilers that it is static (remember: It is static ). That is to say, their addresses are bound during compilation (early binding). Therefore, overloading is irrelevant to polymorphism! "Overwrite" is related to polymorphism ". When the subclass redefined the virtual function of the parent class, the parent class pointer is dynamic based on the different subclass pointer assigned to it (remember: Dynamic !) The call of this function belongs to the subclass. Such a function call cannot be determined during compilation (the address of the virtual function of the subclass called cannot be provided ). Therefore, such a function address is bound at runtime (later bound ). The conclusion is that overload is only a language feature and is irrelevant to polymorphism and object orientation! Reference Bruce Eckel: "Don't be stupid. If it's not late, it's not a polymorphism ."
So what is the role of polymorphism? We know that encapsulation can hide implementation details to modularize the code. inheritance can expand existing code modules (classes). They are all for the purpose of code reuse. Polymorphism aims to achieve another purpose-interface reuse! Polymorphism is used to ensure that a certain attribute of an instance of any type in the family tree is called correctly when the class is inherited and derived.
Generalization(Generalization)
In, the hollow triangle represents the inheritance relationship (class inheritance), in UML In terms ( Generalization ). Person ( Person ) Is a base class, Teacher ( Instructor ) , Student ( Student ) , Guest ( Guest ) Is a subclass.
Logically, if B is a "type" of A, and all functions and attributes of a are meaningful to B, B is allowed to inherit the functions and attributes of.
For example,The teacher is a person,TeacherYesPerson"(A kind).So classTeacherFrom ClassPersonDerived (inherited ).
If a is a base class and B is a derived class of A, B inherits data and functions of.
If Class A and Class B are irrelevant, B cannot inherit the functions and attributes of Class A to make B more functional.
Logically, if B is a kind of a, B is allowed to inherit the functions and attributes of.
Aggregation (combination)
Logically, if A is a part of B, B is not allowed to be born from a, but to combine a with other things.
For example, eye, nose, mouth, and ear are part of the head, therefore, the class head should be composed of the class eye, nose, mouth, and ear, not derived (inherited.
Aggregation types are classified into none and shared (Aggregation), Composite (Combination.
Aggregate (Aggregation)
In the preceding figure, a diamond (hollow) represents aggregation (Aggregation) (The aggregation type is shared), indicating the meaning of aggregationHas-Link. Aggregation is a relatively loose relationship.BNo need to aggregate classesAResponsible.
Combination (Composition)
the only difference between this image and the above is that the diamond is solid, which represents a more robust relationship -- combination ( composition ) (the aggregation type is composite ). has-a , but here, A B control. That is, A with B created with B .
Dependency(Dependency)
HereBAndAIs only a dependency.(Dependency)Link, which indicates that if the classAModified, so the classBWill be affected