Java泛型编译错误 – 类型中的方法方法(Class)不适用于参数

上周四有人在工作时向我展示了一个编译错误,我无法以干净的方式修复它,从那以后它一直困扰着我.

问题是泛型相关,我重新构建了生成编译错误的简化版本的代码.错误发生在下面显示的最后一行代码中.

我一直在寻找各种各样的互联网,但似乎找不到一个合适的解释为什么Java编译器不接受代码.我想如果它允许代码,那么可能会在Bar.operationOnBar()中创建一个类转换问题,但我不知道如何.

有人可以请教我为什么不编译?

public interface Interface {
}


public class Type implements Interface {
}

public class Bar<T> {
    public Bar(Class<T> clazz) {
    }

    public void operationOnBar(Class<T> arg){
    }
}

public class Foo {
    public <T> Bar<T> bar(Class<T> clazz){
        return new Bar<T>(clazz);
    }
    public static void main(String[] args) {
        Class<? extends Interface> extendsInterfaceClazz = Type.class;
        new Foo().bar(extendsInterfaceClazz).operationOnBar(Type.class);
    }
}

在Foo.main()的第二行编译错误:

The method operationOnBar(Class<capture#1-of ? extends Interface>) in the type Bar<capture#1-of ? extends Interface> is not applicable for the arguments (Class<Type>)

顺便说一句.我通过向下转换Type.class来解决它,这样编译器就无法看到Class的泛型类型是“Type”而不是“?extends Interface”.

解决方法:

一点建议:当你不确定为什么编译器禁止某些与泛型相关的转换时,用List< T>替换有问题的泛型类.然后很容易找到打破类型安全的例子.

这种替换是正确的,因为目前Java没有提供一种方法来进行关于泛型类的可能行为的任何小修知识(即它缺乏在声明中指定泛型类的协方差和逆变的方法,如在C#4和Scala中) .因此,Class< T>和列表< T>对于编译器而言,它们的可能行为是等价的,并且编译器必须禁止可能导致List< T>的问题的转换.对于其他泛型类也是如此.

在你的情况下:

public class Bar<T> {
    private List<T> l;

    public Bar(List<T> l) {
        this.l = l;
    }

    public void operationOnBar(List<T> arg) {
        l.addAll(arg);
    }
}

List<Type1> l1 = new ArrayList<Type1>();
List<? extends Interface> l2 = l1;
List<Type2> l3 = Arrays.asList(new Type2());

new Foo().bar(l2).operationOnBar(l3);

Type1 t = l1.get(0); // Oops!
上一篇:java – 在调用超级构造函数之前检查变量的有效性


下一篇:java中为什么不允许类多重继承,却允许接口多重继承