反射练习之越过泛型检查
package ClassTest.ClassFieldsTest; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; /** * FileName: ReflectDemo05 * Author: lps * Date: 2022/4/11 22:24 * Sign:刘品水 Q:1944900433 */ public class ReflectDemo05 { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { //创建集合 ArrayList<Integer> array = new ArrayList<>(); // array.add(10); // array.add(20); // array.add(30); // array.add(Integer.valueOf("lps"));//XXX错误示范 Class<? extends ArrayList> c = array.getClass(); Method add = c.getMethod("add", Object.class); add.invoke(array,"刘品水"); array.add(21); add.invoke(array,"岁"); System.out.println(array); } }