Application of inheritance of Java features
Inheritance is for reuse. Reuse is to reduce redundant code and improve development efficiency.
This time I talk about inheritance, just about a small application in my development, that is, controller log printing. We usually use logs, either SLF or log4j.
To print the corresponding log in the controller, you need to add the following code:
However, if you write this in each class, as the business grows, there will only be more controllers, not less controllers. Is it too redundant to add this code in each class.
So we use the inheritance feature of Java to solve this problem.
Since this code should be used, we can solve this problem by writing an abstract class.
Java developers know that it is up to them to let ordinary classes inherit abstract classes without copying the corresponding methods.
The abstract classes I write are as follows:
My corresponding java code is as follows:
As mentioned above, the redundancy of controller log code segments can be solved by using the inheritance feature of Java.