Java 泛型:无法转换 List<SubClass>列出<SuperClass>? [英] Java Generics: Cannot cast List<SubClass> to List<SuperClass>?
问题描述
刚遇到这个问题:
List<DataNode> a1 = new ArrayList<DataNode>();
List<Tree> b1 = a1; // compile error: incompatible type
其中 DataNode 类型是 Tree 的子类型.
Where the type DataNode is a subtype of Tree.
public class DataNode implements Tree
令我惊讶的是,这适用于数组:
To my surprise, this works for array:
DataNode[] a2 = new DataNode[0];
Tree[] b2 = a2; // this is okay
这个好像有点奇怪.任何人都可以对此做出解释吗?
This likes a bit strange. Can anyone give an explanation on this?
推荐答案
您在第二种情况下看到的是数组协方差.这是 IMO 的一件坏事,它使数组内的赋值变得不安全——它们可能在执行时失败,尽管在编译时没问题.
What you're seeing in the second case is array covariance. It's a bad thing IMO, which makes assignments within the array unsafe - they can fail at execution time, despite being fine at compile time.
在第一种情况下,假设代码确实编译了,然后是:
In the first case, imagine that the code did compile, and was followed by:
b1.add(new SomeOtherTree());
DataNode node = a1.get(0);
你希望发生什么?
你可以这样做:
List<DataNode> a1 = new ArrayList<DataNode>();
List<? extends Tree> b1 = a1;
... 因为那样你只能从 b1
获取东西,而且它们保证与 Tree
兼容.你不能精确地调用 b1.add(...)
因为编译器不知道它是否安全.
... because then you can only fetch things from b1
, and they're guaranteed to be compatible with Tree
. You can't call b1.add(...)
precisely because the compiler won't know whether it's safe or not.
查看Angelika Langer 的 Java 泛型常见问题解答的这一部分了解更多信息.
这篇关于Java 泛型:无法转换 List<SubClass>列出<SuperClass>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!