The three foundations of OO are encapsulation, inheritance, and polymorphism.
These three are ordered, and there is no inheritance without encapsulation, and no polymorphism without inheritance.
[Encapsulation )]
The purpose of encapsulation is to cut the code into many modules, and the interconnectivity between each module is minimized. In this way, there will not be a "Pull-and-pull" situation, reducing the degree of mutual dependency, this reduces the complexity and makes development and maintenance easier.
In fact, no one uses the word "module" as the encapsulation result, but as a "class". The word "module" is used for higher-level packaging purposes. Therefore, we should now regard the "class" as the result of encapsulation and the "module" as many fragments cut out by the entire program. In the OO world, a program generally has multiple modules and one module contains multiple classes.
Encapsulation is based on data. when data is put together, functions that will use the data are also put in.Encapsulation is equivalent to putting data and functions together.
[Visibility )]
Since the purpose of encapsulation is to "reduce the degree of mutual dependency", the problem of visibility is involved: shouldn't this class/method/attribute be exposed to other modules, different classes of the same module, their own derived classes, and friend classes? This is the so-called visibility.
We certainly want to minimize the visibility to reduce the degree of mutual dependency. That is, do not let others know what they do not need. This is called Information Hiding.
The data to be hidden is the data. Some people claim that all data cannot be directly accessed by external entities (including derived classes.
As mentioned above, encapsulate the relevant data and the methods for using the data into classes. The ideal situation is to minimize the visibility of data, which is completely invisible to the outside. Only method is left for the interface. In other words, the interface of each object is a set of methods with no data at all.
However, setting visibility is not easy and requires careful consideration. If the visibility is set too wide, the Information Hiding effect may be poor, which may lead to many negative effects. If the visibility is set too tight, the efficiency and expansion may be deteriorated.
[Inheritance]
The inherited object is called base or parent, and the successor is called a derived class (derived) or child ).
The purpose of inheritance is to achieve "code reuse" or "interface reuse ". The means of inheritance are "expansion" or "modification ". This is the focus of inheritance.
The code reuse caused by inheritance means that the derived class can automatically inherit all the code of the base class, so that you do not need to write too much code, but only need to expand or modify it a little, you can meet your needs. Extension refers to defining a new method, and modification refers to re-defining the behavior of a method in the base class.
(PS: code reuse, which can be achieved through "inheritance" and "Composition. In addition, the "Combination" is recommended for more designs. The combination is dynamic and has better flexibility. The inheritance is more for polymorphism .)
The interface reuse caused by inheritance is in preparation for the next stage (that is, polymorphism) of OO.Interface multiplexing and modification of matching methods form Polymorphism.
If you do not want to reuse code or interfaces, or you do not want to expand or modify the code, it is almost meaningless to generate a derived class through inheritance.
The only small significance is that the derived class and the base class are different classes. You can make judgments based on this in the program and perform different behaviors. However, this is a refined Program Technique (rtti) that has nothing to do with OO, and OO does not encourage you to do so.
When considering inheritance, note that the relationship between the two classes should be "is-. 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.
[Multiple inheritance and interfaces]
A single Inheritance refers to only one base class; multiple inheritance refers to multiple base classes.
Multi-inheritance may cause the unknown Inheritance Method to be troubled by the base class or ancestor class. C ++ requires programmers to specify where the inherited method comes from.
Therefore, multiple inheritance (such as Java) is often prohibited in some languages ).
Since Java, most languages use interfaces to solve the problem of multi-inheritance. But the interface only allows you to inherit from the interface, and cannot inherit from the Code (the interface does not contain code ). Therefore, if you inherit multiple interfaces in Java, you must define each method of all interfaces, that is, you must write a lot of code. But in C ++, You can inherit many classes without defining these methods.
[For the underlying implementation mechanism of C ++ multi-inheritance, please refer to here]
When designing inheritance, you must first consider whether the interface is shared, then consider whether the code is shared, and then consider classification. However, general programmers will first consider classification and code reuse, while ignoring "interface reuse" is the most important thing.
[Polymorphism]
Polymorphism is a technology that allows you to set a parent object to be equal to one or more other sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. Simply put, it is a sentence:Assign a pointer of the subclass type to a pointer of the parent class..
Polymorphism makes people think that objects can appear in multiple ways. When an object has different types, it may lead to polymorphism.
Why is an object of different types? This is because the object can be inherited.PlayRoles of all ancestor classes. For example, if an object's class is a derived class and the object is "transformed" into a base class, the object has two different classes. The "actual class" is a derived class, the "Form class" is a base class. When the method M of this object is called, The method M defined by the base class will be executed? Or is m the method for defining (modifying) A derived class?
The answer is the actual method of the class, that is, the method M defined by the derived class. SoThe so-called polymorphism is: no matter what the form class is, the method of the actual class will be executed.
You may wonder why the object was originally transformed into an ancestor class, resulting in a different "Form class" (Declaration class) from "actual class" (definition class?
Because we want to use one thing (base class) to represent multiple things (derived classes ).
More importantly, a good design isInterface-Oriented Programming. We define interfaces in the base class. In the derived class implementation, different Derived classes can have different implementations. We can change the "actual class" of an interface without changing the user (using the "form class" of the object), and change the behavior of the class. This makes the program easy to modify and expand. [For example, framework.]
Classes can be divided into virtual and non-virtual methods. Only virtual methods can be used together with polymorphism mechanisms. If it is a non-virtual method, the method of the Form class (rather than the actual class) will be executed, because polymorphism does not play a role.
Each language has a different approach to virtualization. Java emphasizes dynamics, so it is virtual by default; C ++ focuses on efficiency, so it is not virtual by default.
Remember:"Interface reuse" is more important than "code reuse. This is because of polymorphism. polymorphism is the ultimate goal of OO..
[Additional Words: heavy load and coverage]
Overwrite, Refers to the sub-class to redefine the virtual function of the parent class. (I don't like the name "Overwrite", so it's easy to confuse people)
Heavy LoadMultiple Functions with the same name are allowed, and the parameter tables of these functions are different.
The implementation of the overload is: the compiler modifies the names of functions with the same name based on different parameter tables of the function, and then these functions with the same name becomeDifferent Functions(At least for the compiler ). 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 !) This function belongs to the subclass,Such a function call cannot be determined during compilation (the virtual function address of the called subclass cannot be provided ). Therefore, such function addresses are bound at runtime (late bond). The conclusion is that overload is only a language feature and is irrelevant to polymorphism and object orientation! Quote Bruce Eckel:"Do not be stupid. If it is not late, it is not a polymorphism."
[Note: This section is adapted from (http://www.cnblogs.com/clongge/archive/2008/07/09/1239076.html)]
[Framework]
To facilitate software development, many software vendors provide application frameworks, such as. NET Framework. With the framework, we can finally enjoy the benefits of OO. We can reuse the code written by others without having to rewrite it by ourselves.
Framework vendors first write a majority of programs, and programmers can apply the entire framework simply by "using inheritance for modification, to ensure that the modified part can be actually executed (rather than the method of the framework itself), all the methods that can be modified are defined as virtual.
Because the programmer "uses inheritance for modification", a derived class and a redefined method are generated. The framework is defined earlier than this derived class. Of course, you do not know this derived class. Therefore, the framework uses the ancestor class of this derived class as a processing object (processing interface ). When the object of this derived class is passed into the framework, it will be automatically transformed into an ancestor class, resulting in the difference between "Form class" and "actual class. Because of such a class difference and the secondary class has a redefinition method, the polymorphism mechanism has emerged.
[Talking About object-oriented]
Process-oriented and object-oriented programming are two different world views. From the process-oriented perspective, it is a process that involves multiple steps to solve a problem. From the object-oriented perspective, it is dependent on the collaboration of related classes to solve a problem.
Object-oriented is an intuitive simulation of the real world. The core issue in object-oriented programs is the relationship between classes.
Different things have different attributes and have different capabilities (class methods ). Messages are transmitted between objects and different responses are made based on the received information. This allows collaboration to solve the problem.
A class is an abstraction of objects with the same attributes. A base class is a base class that extracts commonalities from some Derived classes. Is a high-level abstraction.
Abstraction is a way for humans to understand and transform the world. Humans always try to explain more phenomena with the simplest theory.
Various disciplines and fields also have corresponding theories (such as thermodynamic equations and electromagnetic equations), which are an abstraction of certain scope of things. Newton's universal gravitation is an abstraction of all things. He extracted the commonalities of all things. This is the highest level of abstraction.
(The popular string theory is expected to become a general theory of all theories. It may be the ultimate abstraction of the universe. ^_^)
[Note: The above content can be found on the Internet and in reference books, which are collected on weekdays, sorted out today, and talked about in disorder. As a novice cainiao, the understanding in some places may be problematic. I hope you will not correct me.]