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>,否则会编译出错.