gpt4 book ai didi

java - 使用 java 8 进行回调

转载 作者:行者123 更新时间:2023-11-30 02:42:13 25 4
gpt4 key购买 nike

这个问题有两个部分;

第 1 部分:

我并不完全清楚为什么需要回调,为什么不直接传入一个对象并调用示例代码 C 中的方法,而不是创建一个接口(interface)然后将其传入,如示例代码 A 中所示,或 B。

第 2 部分:

下面给出的三个示例代码有什么区别,A和B有区别吗?就性能和良好实践而言,哪一种最好,为什么 - 即幕后发生的事情导致性能提高,或者为什么在实用性方面最好使用一种而不是另一种。

示例代码A:

interface Iexample{
void foo();
}

public class Roo implements Iexample{
public void foo(){
System.out.println("hello");
}
}

public class Too{

public void eoo(Iexample callback){
callback.foo();
}

public static void main(String[] args){
Roo roo = new Roo();
Too too = new Too();

too.eoo(roo::foo);
}
}

示例代码B(仅main不同):

interface Iexample{
void foo();
}

public class Roo implements Iexample{
public void foo(){
System.out.println("hello");
}
}

public class Too{
public void eoo(Iexample callback){
callback.foo();
}

public static void main(String[] args){
Roo roo = new Roo();
Too too = new Too();

too.eoo(roo);
}
}

示例代码C

public class Roo {
public void foo(){
System.out.println("hello");
}
}

public class Too{
public void eoo(Roo callback){
callback.foo();
}

public static void main(String[] args){
Roo roo = new Roo();
Too too = new Too();

too.eoo(roo);
}
}

编辑:有人愿意解释一下你为什么投反对票吗?如果我知道出了什么问题,我很乐意添加更多信息...

最佳答案

在您的示例中,您不需要回调,但这取决于代码的意图。

就回调而言,您的代码中的含义可能是 Command Pattern 。在 Java 8 之前,您无法将函数作为参数传递,因为不支持 C 或 C++ 中的函数指针。解决方法是命令模式,您将函数调用包装在类中并传递该类,而不是从中调用相应的函数。在您的示例中,接口(interface) Iexample 为没有参数或返回值的方法 foo 定义了一个包装器。

考虑以下示例。你有一个显示 GUI 或其他内容的 main 方法,而后台工作人员则计算其他内容。您有一个输出方法,您想在其中显示当前进度,但它位于您的主类中。现在,您如何将该函数传递给工作线程,以便它可以使用它?您使用命令模式并实现一个包装类来携带如下方法:

interface ProgressCallback {
void progressUpdate(int progress);
}

class BackgroundWorker{

private int progress;
private ProgressCallback callback;

public BackgroundWorker(ProgressCallback progressCallback) {
this.callback = progressCallback;
}

// BackgroundWorker calls to signal the progress
// update to your main application, by calling
// its method passed through the callback wrapper
// class ProgressCallback.
private void notifyProgressUpdateToMain() {
callback.progressUpdate(progress);
}

// Awesome stuff implemented here

}

class Main {

public static void main(String[] args) {

// Implement a new callback "on-the-fly" with the interface
// as template for a progress callback.
ProgressUpdate callback = new ProgressUpdate(int progress) {
@Override
public void progressUpdate() {
// The call of the method of this main class
// is now encapsulated in the callback
setProgressUpdate(progress);
}
};

// Does awesome stuff in background
BackgroundWorker worker = new BackgroudWorker(callback);
worker.start();

// Do other stuff, while the worker sets the progress

}

private setProgressUpdate(int progress) {
// Magically retrieved progress bar from the GUI
progressBar.setProgress(progress);
}

}

您的第一个示例仅使用新的 Java 8 运算符 :: 通过创建匿名包装类来将方法 foo() 作为参数传递。第二个示例可以与早期版本一起使用。该接口(interface)允许您轻松实现不同的回调,如上面的示例所示。第三个示例无法提供这种灵 active ,因为您只能实现单个类 Roo 并且只能对其进行子类化,这不符合您想要在此处实现的目标。

关于java - 使用 java 8 进行回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com