class A1{}
class A2 extends A1{}
class A3 extends A2{}
List<? extends A1> list = new ArrayList<A1>();
list.add(new A2());//error
我无法理解为什么不能添加A1以下的类进去,这应该是安全的。
Thinking in java的中文和英文的那段话我都有看过了,但是还是无法理解其中的意思,请谁解释下好吗? 谢谢!
The wildcard refers to the definite type, so it means "some specify type which the flist reference doesn't specify." So the List that's assigned has to be holding some specify type such as Fruit or Apple, but in order to upcast to flist, that type is a "don't actually care."
class A1{}
class A2 extends A1{}
class A3 extends A2{}
public class Bdemo
{
public static void main(String args[]){
List<Number> list = new ArrayList<Number>();
list.add(3.33f);//正确
list.add(3);//正确
}
}