Looking at work today, the code suddenly has the following ideas:
/** * Created by ZQ on 2017/5/25. * Abstract Classes */public abstract class ABSTRACTC {public void C () { D (); System.out.println ("C_1"); E (); } public abstract void D ();// {// System.out.println ("c_1"),// } public abstract void E ();// {// System.out.println ("c_2");/ }}
Key thing the shadow part of the method C ();
The following code takes advantage of the abstract method of abstract classes to implement the idea of wrapping:
/** * Created by ZQ on 2017/5/25.
* Using abstract methods to implement the idea of packaging */public class Abstractway {public static void Main (String [] args) { new ABSTRACTC () {
@Override public void D () { System.out.println ("c_2"); } @Override public void E () { System.out.println ("C_3"); } } . C (); }}
As above, in fact in the framework of such as Spring,mybatis, there is the idea of this packaging, in spring, this idea is defined as AOP (aspect-oriented programming), such as the implementation of spring annotations (such as: @[email protected]), the code System.out.println ("C_1"); Instead of a "reflection" method, you can perform pre-set business logic, such as access statistics, logging, and so on before and after executing a method.
The exploitation of the characteristics of Java's abstract methods of implementing abstract classes---face-tangent