Callback: When you call a method of Class B in Class A, the Class A invokes a method that has a specific implementation in another class (for example, Class B, usually an interface).
/** Listener Interface */interface listener{public void callbacks (); /** by listening class */class a{ Listener mlistener; Public A () {} public a (Listener Listener) { this.mlistener= Listener; } /** Set Listener * /A Setlistener (Listener Listener) { this.mlistener= Listener; return this; } /** Callback interface * /Void Call () { if (mlistener== null) return; Mlistener.callbacks (); }} /** Main class */public final class main implements listener{public static void main (String []args) { new Main (). Start () ; } private void Start () { new A (). Call ();//null Listener event New A (this). Call (); New A (this). Setlistener (New Listener () {public void callbacks () { System.out.println ("This is new callbacks Method "); } }). Call ();//Reset Listener } /** the specific implementation of the Listener event */public void Callbacks () { System.out.println ("Call Me");} }
Java callback test