gpt4 book ai didi

Java:参数化可运行

转载 作者:太空狗 更新时间:2023-10-29 22:44:11 25 4
gpt4 key购买 nike

标准Runnable接口(interface)只有非参数化run()方法。还有Callable<V>call() 的接口(interface)方法返回泛型类型的结果。我需要传递通用参数,如下所示:

<code>interface MyRunnable<E> {
public abstract void run(E reference);
}</code>
是否有用于此目的的标准接口(interface),或者我必须自己声明那个基本接口(interface)?

最佳答案

通常,您会使用支持通用输入参数的类来实现 RunnableCallable;例如

public class MyRunnable<T> implements Runnable {
private final T t;

public MyRunnable(T t) {
this.t = t;
}

public void run() {
// Reference t.
}
}

关于Java:参数化可运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1659986/

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