抱歉,标题似乎令人困惑,但请按示例进行操作。
假设我有一些带有通用类型参数的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<?>>
凭直觉,我认为所讨论的作业应该不是问题。那么为什么这个分配有问题呢?
您面临的问题称为协方差。
List<GenericClass<String>> stringy = ...
List<GenericClass<?>> generic = stringy;
generic.add(new GenericClass<Integer>());
如果这不是编译错误,那么最后一行代码将是可能的。
您可以通过以下方法解决错误:
List<? extends GenericClass<?>> generic = stringy;
但您也不能使用,add
因为您真的不知道什么? extends GenericClass<?>
(再次是协方差)。在这种情况下,您只能通过List进行枚举并期望GenericClass<?>
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句