Java:参数化的可运行 [英] Java: Parameterized Runnable
本文介绍了Java:参数化的可运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Runnable
接口只有非参数化 run()
方法。还有 Callable< V>
接口,其中 call()
方法返回泛型类型的结果。我需要传递泛型参数,如下所示: interface MyRunnable< E> {
public abstract void run(E reference);
}
是否有用于此目的的标准接口,或者我必须自己声明该基本接口? p>
解决方案
通常,您可以实现 Runnable
或 Callable
作为支持genertic输入参数的类;例如
public class MyRunnable< T>实现Runnable {
private final T t;
public MyRunnable(T t){
this.t = t;
}
public void run(){
//引用t。
}
}
Standard Runnable
interface has only non-parametrized run()
method. There is also Callable<V>
interface with call()
method returning result of generic type. I need to pass generic parameter, something like this:
interface MyRunnable<E> {
public abstract void run(E reference);
}
Is there any standard interface for this purpose or I must declare that basic one by myself?
解决方案
Typically you would implement Runnable
or Callable
as a class that supported a genertic input parameter; e.g.
public class MyRunnable<T> implements Runnable {
private final T t;
public MyRunnable(T t) {
this.t = t;
}
public void run() {
// Reference t.
}
}
这篇关于Java:参数化的可运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文