java 泛型类转型

public class NeedCasting {
    @SuppressWarnings("unchecked")
    public void f(String[] args)throws Exception{
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
        //List<String> list = (List<String>)in.readObject();
        List<String> list2 = List.class.cast(in.readObject());//使用泛型类转型
        
        in.close();
    }
    

}

不能声明为具体的List<String>,否则会编译出错.

posted @ 2019-02-17 22:21  随意的马蒂洛克  阅读(478)  评论(0编辑  收藏  举报