callback function Definition:
Pass a function A to another function B, and this function B executes function A. Let's just say that function A is called a callback function. If there is no name, it is called an anonymous callback function. Or pass a method object A to another method object B, and let the latter execute a at the appropriate time, then a is called a callback function.
The callback function instance understands:
Firemen give a phone number 119 (which everyone knows, 119 is equivalent to an interface in Java), and each place has a fire detection device, if the danger to people's lives, then the public can be 119 to the firemen call, Then call a fireman to handle it. The callback means that the fireman gave us a fire call and we could use this number to call the firemen in the event of a fire, but it was not us who dealt with the problem, but the firemen. We are the equivalent of a feedback, only responsible for the firemen feedback information, and ultimately the handler is a fireman.
The code is as follows:
1. Define the callback interface and the method that will be executed in the callback, that is, the method of dialing a distress call.
Public interface Mycallback { void call ();
2. Define a class that can operate on the callback interface
public class Caller { private mycallback mycallback; public void Docall () { mycallback.call (); } public void Setmycallback (Mycallback mycallback) { this.mycallback = mycallback; } }
3. Instantiate a specific callback function, implement a callback method, tell the fireman to fire the fire
public class Callbacktest {public static void Main (string[] args) { Caller Caller = new Caller (); Caller.setmycallback (New Mycallback () { @Override public Void Call () { System.out.println (' help '); } }); Caller.docall (); } }
callback function Tutorial (i): callback function definition and an instance implemented with an interface