Pair<? extends S>
,Pair能接收的类型参数,是S本身或子类
只能get,不能set,编译器只能保证出来的类型,即只生产,不消费
Pair<? super Apple>
代表Pair<Object>,Pair<Fruit>,Pair<Apple>
等
Pair<Apple> apples = new Pair<>(new Apple(3),new Apple(4));
Pair<? extends Fruit> fruits = apples;
fruits.setFirst(new Orange(5));//编译错误
Pair<? super S>
,Pair能接收的类型参数,是S本身或超类
只能set,不能get,编译器只能保证放入的是S本身或超类,但不保证出来是什么具体类型,即只消费,不生产
Pair<? super Apple>
代表Pair<Object>,Pair<Fruit>,Pair<Apple>
等
Pair<? super Apple> fruits = new Pair<Fruit>();
fruits.setFirst(new Apple(5));//Apple到超类的转型
fruits.setSecond(new GreenApple(5));//GreenApple到超类的转型
Fruit obj = fruits.getFirst();//报错,未知其超类对象的方法
fruits.getFirst().hello();//报错,未知其超类对象的方法