package cn.itcast2;
//绕过ArrayList<Integer>的一个对象的泛型检查,在集合中添加一个字符串数据。只需要将add方法使用反射
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) throws Exception, Exception {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList .add(13);
arrayList.add(22);
Class class1 = arrayList .getClass();
Method declaredMethod = class1.getDeclaredMethod("add", Object.class);
declaredMethod.invoke(arrayList, "asd");
System.out.println(arrayList);
}
}
|
|