Java programming ideology note (3) -- reuse of classes and reuse of programming ideology
I. Introduction to reuse Functions
Code reuse is not enough to copy and change the code, but also to use classes without damaging the existing program code.
In java, code reuse can be divided into the following three types: combination, inheritance, and proxy.
Ii. Combination
The new class uses the objects of the existing class. The new class consists of the objects of the existing class.
Iii. Inheritance
Declaration is implemented using the extends syntax.
Iv. Proxy
Java does not provide direct support for proxies, but indirect implementation is achieved through combination and inheritance. proxies are the moderate way of combination and inheritance. Proxy = inheritance + combination
For more information, see the relevant code!
5. Basic Method: quilt overload
The method used by the subclass to overload the parent class does not block any version of the base class. Therefore, the overload mechanism can be normally executed whether the method is defined in this layer or its base class.
6. Discussion on combination and inheritance
Emphasizing inheritance does not mean we should use inheritance as much as possible. On the contrary, we should use inheritance with caution. The clearest way to determine whether to use inheritance or combination is to ask whether you need to transform from a new class to a base class.