我有一个这样声明的列表:
List<? extends Number> foo3 = new ArrayList<Integer>();
我试图将 3 添加到 foo3。但是我收到这样的错误消息:
The method add(capture#1-of ? extends Number) in the type List<capture#1-of ? extends Number> is not applicable for the arguments (ExtendsNumber)
对不起,但你不能。
的通配符声明List<? extends Number> foo3意味着变量foo3可以保存来自类型族的任何值(而不是特定类型的任何值)。这意味着其中任何一个都是合法的分配:
List<? extends Number> foo3
foo3
List<? extends Number> foo3 = new ArrayList<Number>(); // Number "extends" Number List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number List<? extends Number> foo3 = new ArrayList<Double>(); // Double extends Number
因此,鉴于此,在上述任何可能的分配之后,您可以添加什么类型的对象List foo3是合法的:ArrayList
List foo3
ArrayList
Integer
List<Double>
Double
List<Integer>
Number
你不能添加任何对象,List<? extends T>因为你不能保证List它真正指向的是什么类型,所以你不能保证那个对象是允许的List。唯一的“保证”是您只能从中读取,并且您将T获得T.
List<? extends T>
List
T
反向逻辑适用于super,例如List<? super T>。这些是合法的:
super
List<? super T>
List<? super Number> foo3 = new ArrayList<Number>(); // Number is a "super" of Number List<? super Number> foo3 = new ArrayList<Object>(); // Object is a "super" of Number
您无法从中读取特定类型 T (例如Number),List<? super T>因为您无法保证List它真正指向的是哪种类型。您拥有的唯一“保证”是您能够添加类型T(或任何子类T)的值,而不会违反所指向列表的完整性。
完美的例子是签名Collections.copy():
Collections.copy()
public static <T> void copy(List<? super T> dest, List<? extends T> src)
请注意src列表声明如何extends用于允许我从相关列表类型系列中传递任何列表,并仍然保证它将产生类型 T 或 T 子类的值。但您不能添加到src列表中。
src
extends
dest列表声明用于super允许我从一系列相关列表类型中传递任何列表,并且仍然保证我可以将特定类型 T 的值写入该列表。但是如果我从列表中读取,则不能保证读取 特定类型 T 的值。
dest
所以现在,多亏了泛型通配符,我可以使用该单一方法进行任何这些调用:
// copy(dest, src) Collections.copy(new ArrayList<Number>(), new ArrayList<Number>()); Collections.copy(new ArrayList<Number>(), new ArrayList<Integer>()); Collections.copy(new ArrayList<Object>(), new ArrayList<Number>()); Collections.copy(new ArrayList<Object>(), new ArrayList<Double>());
考虑一下这个令人困惑且非常广泛的代码来锻炼你的大脑。注释掉的行是非法的,原因在该行的最右边(需要滚动查看其中一些):
List<Number> listNumber_ListNumber = new ArrayList<Number>(); //List<Number> listNumber_ListInteger = new ArrayList<Integer>(); // error - can assign only exactly <Number> //List<Number> listNumber_ListDouble = new ArrayList<Double>(); // error - can assign only exactly <Number> List<? extends Number> listExtendsNumber_ListNumber = new ArrayList<Number>(); List<? extends Number> listExtendsNumber_ListInteger = new ArrayList<Integer>(); List<? extends Number> listExtendsNumber_ListDouble = new ArrayList<Double>(); List<? super Number> listSuperNumber_ListNumber = new ArrayList<Number>(); //List<? super Number> listSuperNumber_ListInteger = new ArrayList<Integer>(); // error - Integer is not superclass of Number //List<? super Number> listSuperNumber_ListDouble = new ArrayList<Double>(); // error - Double is not superclass of Number //List<Integer> listInteger_ListNumber = new ArrayList<Number>(); // error - can assign only exactly <Integer> List<Integer> listInteger_ListInteger = new ArrayList<Integer>(); //List<Integer> listInteger_ListDouble = new ArrayList<Double>(); // error - can assign only exactly <Integer> //List<? extends Integer> listExtendsInteger_ListNumber = new ArrayList<Number>(); // error - Number is not a subclass of Integer List<? extends Integer> listExtendsInteger_ListInteger = new ArrayList<Integer>(); //List<? extends Integer> listExtendsInteger_ListDouble = new ArrayList<Double>(); // error - Double is not a subclass of Integer List<? super Integer> listSuperInteger_ListNumber = new ArrayList<Number>(); List<? super Integer> listSuperInteger_ListInteger = new ArrayList<Integer>(); //List<? super Integer> listSuperInteger_ListDouble = new ArrayList<Double>(); // error - Double is not a superclass of Integer listNumber_ListNumber.add(3); // ok - allowed to add Integer to exactly List<Number> // These next 3 are compile errors for the same reason: // You don't know what kind of List<T> is really // being referenced - it may not be able to hold an Integer. // You can't add anything (not Object, Number, Integer, // nor Double) to List<? extends Number> //listExtendsNumber_ListNumber.add(3); // error - can't add Integer to *possible* List<Double>, even though it is really List<Number> //listExtendsNumber_ListInteger.add(3); // error - can't add Integer to *possible* List<Double>, even though it is really List<Integer> //listExtendsNumber_ListDouble.add(3); // error - can't add Integer to *possible* List<Double>, especially since it is really List<Double> listSuperNumber_ListNumber.add(3); // ok - allowed to add Integer to List<Number> or List<Object> listInteger_ListInteger.add(3); // ok - allowed to add Integer to exactly List<Integer> (duh) // This fails for same reason above - you can't // guarantee what kind of List the var is really // pointing to //listExtendsInteger_ListInteger.add(3); // error - can't add Integer to *possible* List<X> that is only allowed to hold X's listSuperInteger_ListNumber.add(3); // ok - allowed to add Integer to List<Integer>, List<Number>, or List<Object> listSuperInteger_ListInteger.add(3); // ok - allowed to add Integer to List<Integer>, List<Number>, or List<Object>