什么是java中的回调方法? 术语似乎松散使用

我不明白回调方法是什么,我听说人们非常松散地使用这个术语。 在java世界中,什么是回调方法? 如果有人可以提供带有解释的java回调方法的示例代码,那么在我的java学习之旅中它会有很大的帮助吗?

先谢谢你。

回调是一段代码,您将其作为参数传递给其他代码,以便它执行它。 由于Java尚不支持函数指针,因此它们实现为Command对象。 就像是

public class Test { public static void main(String[] args) throws Exception { new Test().doWork(new Callback() { // implementing class @Override public void call() { System.out.println("callback called"); } }); } public void doWork(Callback callback) { System.out.println("doing work"); callback.call(); } public interface Callback { void call(); } } 

回调通常会引用一些实际有用的状态。

通过使回调实现具有对代码的所有依赖性,您可以获得代码与执行回调的代码之间的间接。

java中的回调方法是在发生事件(称为E )时调用的方法。 通常,您可以通过将某个接口的实现传递给负责触发事件E的系统来实现它(参见示例1)。

此外,在更大,更复杂的系统中,您只需注释一个方法,系统就会识别所有带注释的方法,并在事件发生时调用它们(参见示例2)。 当然,系统定义了方法应该接收的参数和其他约束。

例1:

 public interface Callback { //parameters can be of any types, depending on the event defined void callbackMethod(String aParameter); } public class CallbackImpl implements Callback { void callbackMethod(String aParameter) { //here you do your logic with the received paratemers //System.out.println("Parameter received: " + aParameter); } } //.... and then somewhere you have to tell the system to add the callback method //eg systemInstance.addCallback(new CallbackImpl()); 

例2:

 //by annotating a method with this annotation, the system will know which method it should call. @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CallbackAnnotation {} public class AClass { @CallbackAnnotation void callbackMethod(String aParameter) { //here you do your logic with the received paratemers //System.out.println("Parameter received: " + aParameter); } } //.... and then somewhere you have to tell the system to add the callback class //and the system will create an instance of the callback class //eg systemInstance.addCallbackClass(AClass.class); 

在简单的单词中……在简单的英语中,回调函数就像一个工人,当他完成一个任务时“回调”给他的经理。

它们与从调用函数中获取某些上下文的另一个函数调用一个函数有什么不同? 确实,您正在从另一个函数调用函数,但关键是回调被视为对象,因此您可以根据系统状态(如策略设计模式)更改要调用的函数。

如何向新手程序员解释他们的权力? 在需要从服务器提取数据的AJAX风格的网站中可以很容易地看到回调的强大function。 下载新数据可能需要一些时间。 如果没有回调,您的整个用户界面将在下载新数据时“冻结”,或者您需要刷新整个页面而不仅仅是其中的一部分。 使用回调,您可以插入“正在加载”图像,并在加载后将其替换为新数据。

一些没有回调的代码:

  function grabAndFreeze() { showNowLoading(true); var jsondata = getData('http://yourserver.com/data/messages.json'); /* User Interface 'freezes' while getting data */ processData(jsondata); showNowLoading(false); do_other_stuff(); // not called until data fully downloaded } function processData(jsondata) { // do something with the data var count = jsondata.results ? jsondata.results.length : 0; $('#counter_messages').text(['Fetched', count, 'new items'].join(' ')); $('#results_messages').html(jsondata.results || '(no new messages)'); } 

使用回调:这是一个回调的例子,使用jQuery的getJSON:

  function processDataCB(jsondata) { // callback: update UI with results showNowLoading(false); var count = jsondata.results ? jsondata.results.length : 0; $('#counter_messages').text(['Fetched', count, 'new items'].join(' ')); $('#results_messages').html(jsondata.results || '(no new messages)'); } ` `function grabAndGo() { // and don't freeze showNowLoading(true); $('#results_messages').html(now_loading_image); $.getJSON("http://yourserver.com/data/messages.json", processDataCB); /* Call processDataCB when data is downloaded, no frozen User Interface! do_other_stuff(); // called immediately