代码完成编译后,编译器丢弃类型参数的类型信息;因此,此类型信息在运行时是不可用的。
java的泛型是伪泛型,编译后java虚拟机就认不得泛型附加的信息了。
假如student类是people类的一个子类,如下定义:
这个时候我们构造一个方法的参数是List
如上图,由于类型会被擦除,不论用什么类型,只有该类型的参数可以作为参数,而当用通配符?时,所有类型的列表都可以传入,但此时方法内不能有涉及到参数类型的操作(jvm不知道参数的类型)。
注意前后差距,多了extens,表明这里只能传入people和people的子类型。如下图,传入了List 类型的列表,提示错误。
同样,若限制只能传入people以及people的父类,则需添加super people修饰:
static void fun(List super people> my){
for(Object temp:my){
System.out.println(temp);
}
}
public static void main(String[] args) {
List list=new ArrayList();
fun(list);
}
源码
import java.util.ArrayList;
import java.util.List;
public class Compare {
List mylist=new ArrayList<>();
static void fun(List super people> my){
for(Object temp:my){
System.out.println(temp);
}
}
public static void main(String[] args) {
List list=new ArrayList();
fun(list);
}
}
class creature{
}
class people extends creature{
String name;
}
class animal extends creature{
String name;
}
class student extends people{
String school;
public student(String school) {
this.school = school;
}
}



