Object-oriented three pillars of the spirit, the first is "inheritance"!
Inheritance from a code reuse point of view, particularly useful, but also particularly vulnerable to abuse and misuse. One of the greatest characteristics that is caused by improper use of inheritance is high coupling (to add that coupling is a feature, although most cases are characteristic of defects, but coupling is not a defect when coupling becomes a requirement)
Code reuse is also classified, and it is inappropriate to use inheritance if it was only for the purpose of code reuse, without distinguishing between categories and scenes.
For most of the current development tasks, the occurrence of inheritance is not many scenes, mainly code reuse of the scene is more, but through the combination of code reuse appears to be more troublesome than inheritance, because the combination requires you to have a stronger abstraction, inheritance is more in line with intuition. However, in view of the possible changes in demand and maintenance costs in the future, the use of portfolios is actually worthwhile. In addition, when you find that you inherit more than 2 layers, you have to think about whether this succession plan , third-level inheritance is the beginning of abuse. After you have determined that it is necessary, proceed to more levels of inheritance.
Inheritance is a model of tight coupling, the main embodiment is to hold the whole body. Embodied in two areas:
(1) The first type of problem is changed one place, everywhere to change, but the solution is very convenient
(2) The second type of problem is when the code is reused , and then the parent class and the parent class all related dependencies are also copied past, high coupling in the reuse of the resulting redundancy.
The solution is: replace inheritance with a combination!
In fact, most of the code is used to choose the inheritance of the case, in fact, is a combination of better.
When will it be better to use inheritance?
It seems that there is no place to use inheritance is not. But in fact, using inheritance, we have to divide the hierarchy, the use of inheritance is actually how to assign a hierarchy of objects. In the right way of inheritance, the parent class should play the underlying role, and the subclass is the upper-level business.
Three main points of using inheritance
Point 1: The parent class only provides services to subclasses and does not involve the business logic of subclasses
Point 2: The hierarchical relationship is obvious, the function is clearly divided, and the parent class and subclass do each. Actually, it's almost like 1 points.
Point 3: All changes to the parent class need to be reflected in the subclass, that is, the coupling has become a requirement, after all, inheritance is the is-a relationship
An experienced person's attitude is: The last resort not to inherit, priority to consider the combination ! This is precisely the point:"combination is greater than inheritance" (Composition over inheritance) !
Finally on the net dang down a picture, you see what feel?
On the understanding of object-oriented "inheritance"