【发布时间】:2016-08-10 15:18:55
【问题描述】:
我的 IDE 抱怨“NCM_Callable 无法在这一行 this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); In the "fetchDevices()" method
上转换为 Callable<ReturnInterface<? extends Object>>
我只想能够将 Callables 传递给我的 ecs,它返回一个包含任何类型对象的 ReturnInterface。
我怀疑我对 泛型定义的使用有问题,但我似乎无法弄清楚它是什么。任何帮助,将不胜感激。
@Override
public void fetchDevices() throws Exception {
System.out.println("[NCM_Fetcher]fetchingDevices()");
for (int i = 0; i < this.deviceGroupNames.size(); i++) {
System.out.println("[NCM_Fetcher]fetchingDevices().submitting DeviceGroup Callable " + i+ " of "+this.deviceGroupNames.size());
this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph));
}
this.ph.execute();//See progressBarhelper below
}
ProgressBarHelper:我在“ecs.submit()”中有一个奇怪的错误。从我读过的内容来看,似乎我可能需要一个辅助方法?我该如何解决?
public class ProgressBarHelper extends SwingWorker<Void, ReturnInterface> implements ActionListener {
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
protected CompletionService<ReturnInterface<?>> ecs;
public final void submitCallable(Callable<? extends ReturnInterface<?>> c) {
//create a map for this future
ecs.submit(c);
this.callables.add(c);//Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>>
System.out.println("[ProgressBarHelper]submitted");
}
}
最后是 NCM_Callable 类及其泛型。
public class NCM_Callable implements Callable<ReturnInterface<ResultSet>>, ReturnInterface<ResultSet> {
【问题讨论】:
-
首先创建一个minimal reproducible example。您可能会在此过程中解决您的问题。
-
不是重复的。
-
它解决了你的问题吗?查看第二个答案,您必须将方法签名更改为使用
Callable<? extends ReturnInterface<? extends Object>> c -
顺便说一句,
<? extends Object>和刚才的<?>是一样的