recently studying spring source, there is a callback function concept, has been appearing, and the frequency is very large, small series on the search for a lot of information, but always feel less practical, and not easy to understand, small series combined with their own deeds, combined with life, did so several pieces of code.
first, the callback function is the method C of Class A that calls Class B, and then Class B, in turn, calls Method D in Class A, which is the legendary callback function. The feeling is still very abstract, directly to the example bar.
callback function Interface:
Package springcallback;
/**
* This is a callback interface
* @author XZP * */public
interface CallBack {
/**
* This is a small super think about the function to call the small Xu, that is, the callback function
* @param result is the answer
/public
void Solve (String results);
One of the classes:
Package springcallback;
/** *
This is Xiao Xu
* @author XZP * */Public
class Xu implements CallBack {
/**
* Xiao Li object reference
* Equivalent to-----> background Two
* *
private Xc Xc;
/**
* Small Xu's construction method, holds the small super reference * @param XC * * * The public
Xu (XC XC) {
this.xc = XC;
}
/**
* Xiao Xu through this method to ask the problem of small super
* @param question is small Xu to ask questions, do my girlfriend good
? /Public
void Askquestion (final String question) {
//This is asynchronous with one thread,
new Thread (new Runnable () {
@ Override public
Void Run () {
/**
* Small Xu calls the method in the mini-super, where the callback interface is registered
* This is equivalent to the method
of a Class A calling b * * * Xc.executemessage (xu.this, question);
}
}). Start ();
Xiao Xu after the question, went to play basketball. Play
();
}
public void Plays () {
System.out.println ("I'm going to play basketball");
}
/**
* Small Super Consider after the call this method to tell Xiao Xu, is called the small Xu's callback method
* *
@Override public
void Solve (String result) {
System.out.println ("Small super tells Xiao Xu the answer is--->" + result);
}
another class:
Package springcallback;
/**
* This is Xiao Xu *
@author XZP * */Public
class Xc {
/**
* *
* @param callBack
* @param Question small Xu Quan Problem
* * Public
void ExecuteMessage (CallBack CallBack, String question) {
SYSTEM.OUT.PRINTLN ("The problem of small Xu Quan--->" + question);
Analog little super Going to sleep, joined in sleep for a long time for
(int i=0; i<2000000000;i++) {
}
/**
* Small Super Wake up thinking is good, agree ...
*/
String result = "OK, agree ..." ";
/**
* So tell Xiao Xu, call the method in the small Xu
* This is equivalent to the B class in turn to call a method d
* *
callback.solve (result);
}
Test class:
Package springcallback;
/**
* Test class
* @author XZP
* * *
/public class Test {public
static void Main (String[]args) { c39/>/**
* Instantiation of the Little super new A small
super
/Xc Xc = new Xc ();
/**
* Calls the construction method of Xiao Xu, plus the reference of small super
/Xu Xu = new Xu (XC);
/**
* Small Xu asked small super question
/xu.askquestion ("Do my girlfriend good ....") ");
}
}
Execution Results:
above is just some of their own understanding, about the callback function, I personally understand or a bit deficient, through the callback function, two classes of direct communication, each other will not have much impact on the completion of the call.