为什么不能将包含泛型类型的泛型类型分配给泛型类型的泛型类型 [英] Why can't a Generic type containing a Generic type be assigned to a Generic typed class of wildcard type

查看:246
本文介绍了为什么不能将包含泛型类型的泛型类型分配给泛型类型的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



比方说,我有一些具有泛型类型参数的Java类:

  public class GenericClass< T> {
}

我可以创建一个类型变量来存储一个对象,设置为,比如 String 。 Java也允许我将该变量赋值给另一个变量,但通用参数设置为通配符<?> 类型:

  GenericClass< String> stringy = ... 
GenericClass<?> generic = stringy; //确定

但是,在使用泛型参数的类时,如果将类型该参数是通用的,则不能将该类的对象分配给相同类型/通用化类型,其中后者(内部/嵌套)参数是通配符类型<?>

  GenericClass< GenericClass< String>> stringy = ... 
GenericClass< GenericClass<>> generic = stringy; //编译错误

//为了防止混淆,还需要一个
//逼真的例子:
List< GenericClass< String>> stringy = ...
List< GenericClass<>> generic = stringy; //编译错误

具体的编译错误是:

 类型不匹配:无法从List< GenericClass< String>>转换列出< GenericClass<>> 

直觉上来说,我认为有问题的分配不应该成为问题。那么,为什么这个任务是一个问题呢?

解决方案

您的面临的问题被命名为 Covariance

  List< GenericClass<字符串>> stringy = ... 
List< GenericClass<>> generic = stringy;
generic.add(new GenericClass< Integer>());

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

您可以这样做来避免错误:

  List< ;?扩展了GenericClass<>>> generic = stringy; 

但您也不能使用 add ,因为你真的不知道什么?扩展GenericClass<?> is(Covariance再一次)。在这种情况下,您只能枚举列表并期望 GenericClass<>


Sorry if the title seems confusing, but some examples are in order.

Let's say I have some Java class with a generic type parameter:

public class GenericClass<T> {
}

I can create a variable typed to store an object, with the generic parameter set to, say a String. Java will also let me assign that variable to another variable but with the generic parameter set to the wildcard <?> type:

GenericClass<String> stringy = ...
GenericClass<?> generic = stringy; // OK

However, when working with a class with a generic parameter, if you set the type of that parameter to be generic, you can't then assign an object of that class to an identically typed/genericized type where the latter (inner/nested) parameter is of the wildcard type <?>:

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

The specific compile error is:

Type mismatch: cannot convert from List<GenericClass<String>> to List<GenericClass<?>>

Intuitively I would think that the assignment in question should not be a problem. So why is this assignment an problem?

解决方案

The problem your facing is denominated Covariance.

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

If this wasn't a compile error, then the last line of code would be possible.

You could get around the error by doing this:

 List<? extends GenericClass<?>> generic = stringy;

but you can not use add also because you don't really know what ? extends GenericClass<?> is (Covariance once again). In this case you can only enumerate through the List and expect GenericClass<?>.

这篇关于为什么不能将包含泛型类型的泛型类型分配给泛型类型的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆