Java 泛型导致问题

标签 java generics

我的 IDE 提示“NCM_Callable 无法在此行 Callable<ReturnInterface<? extends Object>> 上转换为 this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); In the "fetchDevices()" method

我只是希望能够将 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> {

最佳答案

据我检查,有问题的行是ecs.submit:

public final void submitCallable(Callable<? extends ReturnInterface<?>> c) {
    // create a map for this future
    ecs.submit(c); // Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>>
   this.callables.add(c); // this is fine on my machine
}

问题是java泛型是invariant 。有一个解决方法:您可以将通配符替换为命名的绑定(bind)类型:

public final <T extends ReturnInterface<?>> void // this is how you name and bind the type
   submitCallable(Callable<T> c) { // here you are referring to it
    // create a map for this future
    ecs.submit((Callable<ReturnInterface<?>>) c); // here you need to cast. 
    ...
}

由于type erasure,在运行时转换没问题。 。但接下来要非常小心地处理这些Callables

最后,这是调用此函数的方法:

private static class ReturnInterfaceImpl implements ReturnInterface<String>  {

};

public void foo() {
    Callable<ReturnInterfaceImpl> c = new Callable<ReturnInterfaceImpl>() {
        @Override
        public ReturnInterfaceImpl call() throws Exception {
            return null;
        }
    };
    submitCallable(c);
}

关于Java 泛型导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877488/

相关文章:

java - 如何让exoplayer的ClippingMediaSource更加精准?

java - Tomcat进程阻止其他进程重新绑定(bind)到porton重启

c# - 为什么这在代码 'IResult<H>'

C# 具有多种类型的泛型列表

java - 尝试从文本文件打印列表?

java - 如何修复空指针异常

c# - 泛型方法的 Type.GetMethod()

c# - 调用泛型方法的类型数组

Java泛型方法——如何让它调用专门的方法

java - 在 Kotlin 中实现 (/inherit/~extend) 注解