【Java SE】? extends T 和 ? super T
class Pair<T> { T value; public T getFirst() { return value; } public void setFirst(T value) { this.value = value; } } class Person { } class Employee extends Person { } class Manager extends Employee { } Person person = new Person(); Employee employee = new Employee(); Manager manager = new Manager(); Pair<? super Employee> p1 = new Pair<>(); p1.setFirst(employee);// 存放自己和子类 Employee e1 = p1.getFirst();// 错误,超类限定符get失效 Pair<? extends Employee> p2 = new Pair<>(); p2.setFirst(employee);// 错误,子类限定符set失效 Employee e2 = p2.getFirst();// 接收自己和父类
? super T 超类限定符,下界限定符,导致get失效
元素是T的父类,
? extends T 子类限定符,上界限定符,导致set失效