public class ArrayListDemo {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
// list.add("hello");不能添加
Class clzz = list.getClass();
Method method = clzz.getMethod("add",Object.class);
method.invoke(list,"hello");
System.out.println(list);
}
}
绕过泛型,通过反射把 String 添加到 List < Integer > 中
发布于 2021-05-25 198 次阅读
Comments | NOTHING