标准Runnable
接口(interface)只有非参数化run()
方法。还有Callable<V>
与 call()
的接口(interface)方法返回泛型类型的结果。我需要传递通用参数,如下所示:
<code>interface MyRunnable<E> {
public abstract void run(E reference);
}</code>
是否有用于此目的的标准接口(interface),或者我必须自己声明那个基本接口(interface)?
最佳答案
通常,您会使用支持通用输入参数的类来实现 Runnable
或 Callable
;例如
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/