public class Demo1 {
public static void main(String[] args) throws Exception {
// 给出上限,只能get(),不能add()
ArrayList<? extends B> exObject = new ArrayList<>();
B b = exObject.get(0);
// 给出下限,只能add()B及B的子类,get出的都是Object
ArrayList<? super B> suObject = new ArrayList<>();
suObject.add(new C());
Object object = suObject.get(0);
}
}
class A {
}
class B extends A {
}
class C extends B {
}
<? extends B>和<? super B>
于 2022-02-08 15:36:16 首次发布