Java Callback Preliminary learning

Source: Internet
Author: User

Reprint please indicate this article from Xiaanming's blog (http://blog.csdn.net/xiaanming/article/details/17483273), please respect the results of other people's hard work, thank you
Previously did not understand what called the callback, every day listen to people say add a callback method What, my heart think I grass, what is called callback method ah? And then I find it on the Internet Ah find, find a lot is not very clear, now know, called callback: is a class called in Class B of a method C, and then in the Class B in turn called the method in Class A d,d this method called callback method, so that you are not a bit dizzy, in fact, I just started to understand , looked at the other people say the more classic callback method:
Class a implements interface callback callback--background 1
Class A contains a reference to Class B b--background 2
Class B has a method with a parameter of CallBack F (CallBack CallBack)--Background 3
Object A of a calls B method F (CallBack CallBack)--a class calls a method of Class B
Then B can call a method in the F (CallBack CallBack) method--b class calls a method of Class D
We all like to use the phone example, well, in order to keep up with the times, I also use this example, I use this example of asynchronous plus callback
One day Xiao Wang encountered a difficult problem, the question is "1 + 1 =?", on the phone to ask Xiao Li, Xiao Li suddenly did not know, and Xiao Wang said, and so I finish the things on hand, to think about the answer, Xiao Wang will not be silly to take the phone to wait for Xiao Li's answer, so Xiao Wang said to Xiao Li, I have to go shopping, You know the answer to call me to tell me, so hang up the phone, do their own things, after one hours, Xiao Li hit the small Wang's phone, told him the answer is 2

/**
* This is a callback interface
* @author xiaanming
*
*/
Public interface CallBack {
/**
* This is the function that Xiao Li will call when he knows the answer. Tell Xiao Wang, the callback function
* @param result is the answer
*/
public void Solve (String result);
}


/**
* This is Xiao Wang
* @author xiaanming
* Implemented a callback interface callback, equivalent to-----> background A
*/
public class Wang implements CallBack {
/**
* Reference to Xiao Li object
* Equivalent to-----> Background II
*/
Private Li Li;

/**
* The construction method of Xiao Wang, holding the reference of Xiao Li
* @param li
*/
Public Wang (Li Li) {
This.li = Li;
}

/**
* Xiao Wang used this method to ask Xiao Li's question.
* @param question is the question that Xiao Wang wants to ask, 1 + 1 =?
*/
public void Askquestion (final String question) {
Here a thread is asynchronous,
New Thread (New Runnable () {
@Override
public void Run () {
/**
* Xiao Wang calls the method of small Lee, register callback interface here
* This is equivalent to method C of Class A call B
*/
Li.executemessage (wang.this, question);
}
}). Start ();

The net asks the question to hang up the telephone to do other things, exaggeration street.
Play ();
}

public void Play () {
System.out.println ("I'm going to go shopping");
}

/**
* Xiao Li know the answer after calling this method to tell Xiao Wang, is the so-called Xiao Wang's callback method
*/
@Override
public void Solve (String result) {
System.out.println ("Xiao Li told Xiao Wang the answer is--->" + result);
}

}


/**
* This is Xiao Li.
* @author xiaanming
*
*/
public class Li {
/**
* Equivalent to Class B with parameters of callback callback F ()----> Background Three
* @param callBack
* @param question Wang asked the question
*/
public void ExecuteMessage (CallBack CallBack, String question) {
System.out.println ("Xiao Wang asked questions--->" + question);

It takes a long time to simulate Xiao Li's own affairs.
for (int i=0; i<10000;i++) {

}

/**
* When Xiao Li finishes his own business, he thinks the answer is 2.
*/
String result = "answer is 2";

/**
So I called Xiao Wang and called Xiao Wang's method
* This is equivalent to Class B, which in turn calls a method D
*/
Callback.solve (result);



}

}


/**
* Test class
* @author xiaanming
*
*/
public class Test {
public static void Main (String[]args) {
/**
* New A little Lee
*/
Li li = new Li ();

/**
* New A Little King *
*/
Wang Wang = new Wang (LI);

/**
* Xiao Wang asked Xiao Li's question
*/
Wang.askquestion ("1 + 1 =?");
}
}

Another easy to understand: http://www.importnew.com/19301.html

Java Callback Preliminary learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.