抱歉,标题似乎令人困惑,但请按顺序排列一些示例。

假设我有一些带有通用类型参数的Java类:

public class GenericClass<T> {
}

我可以创建一个类型为存储对象的变量,并将通用参数设置为String。 Java还允许我将该变量分配给另一个变量,但将通用参数设置为通配符<?>类型:
GenericClass<String> stringy = ...
GenericClass<?> generic = stringy; // OK

但是,在使用具有泛型参数的类时,如果将该参数的类型设置为泛型,则无法将该类的对象分配给相同类型/泛型的类型,后者(内部/嵌套)参数是通配符类型<?>:
GenericClass<GenericClass<String>> stringy = ...
GenericClass<GenericClass<?>> generic = stringy; // Compile Error

// And just in case that is confusing, a more
// realistic example involving Collections:
List<GenericClass<String>> stringy = ...
List<GenericClass<?>> generic = stringy; // Compile Error

具体的编译错误是:
Type mismatch: cannot convert from List<GenericClass<String>> to List<GenericClass<?>>

凭直觉,我认为所讨论的任务应该不成问题。那么为什么这个分配有问题呢?

最佳答案

您面临的问题称为Covariance

List<GenericClass<String>> stringy = ...
List<GenericClass<?>> generic = stringy;
generic.add(new GenericClass<Integer>());

如果这不是编译错误,那么最后一行代码将是可能的。

您可以通过执行以下操作来解决该错误:
 List<? extends GenericClass<?>> generic = stringy;

但您也不能使用add,因为您实际上并不知道? extends GenericClass<?>是什么(再次是协方差)。在这种情况下,您只能枚举List并期望GenericClass<?>

09-30 18:06
查看更多