Why second example compiles sucessfuly
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.*; class Test { public static class Base { } public static class Derived1 extends Base { } public static class Derived2 extends Base { } public static void main (String[] args) { //Example1. List<? extends Base> e = new ArrayList<Base>(); e.add(new Derived1()); //this won't compile //Example2. List<? super Base> b = new ArrayList<Base>(); b.add(new Derived1()); //this compiles } } |
另一方面,
看什么是PECS(生产者扩展消费者超级)?.
如果您要向