类型擦除与解决方案

类型擦除

代码完成编译后,编译器丢弃类型参数的类型信息;因此,此类型信息在运行时是不可用的。

java的泛型是伪泛型,编译后java虚拟机就认不得泛型附加的信息了。

假如student类是people类的一个子类,如下定义:

  

这个时候我们构造一个方法的参数是List<people>类型的my, 这在虚拟机看来就是一个List类型,如果我们向函数传入了List<student>类型的一个列表,编译器就会报错(由于类型擦除,编译器已经“六亲不认”,不晓得student是people的子类型了)

解决方案

如上图,由于类型会被擦除,不论用什么类型,只有该类型的参数可以作为参数,而当用通配符?时,所有类型的列表都可以传入,但此时方法内不能有涉及到参数类型的操作(jvm不知道参数的类型)。

注意前后差距,多了extens,表明这里只能传入people和people的子类型。如下图,传入了List<animal> 类型的列表,提示错误。

 同样,若限制只能传入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<creature> list=new ArrayList<creature>();
        fun(list);
    }

源码

import java.util.ArrayList;
import java.util.List;

public class Compare {
    List<String> 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<creature> list=new ArrayList<creature>();
        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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_16198739/article/details/125173218