java类反射越过泛型检查


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

//通过反射越过泛型检查
public class ReflectDemo6 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(10);
        integers.add(20);
        integers.add(30);
//        不能通过检查
//        integers.add("hello");

        //通过反射越过检查
        Class<? extends ArrayList> integersClass = integers.getClass();
        Method add = integersClass.getMethod("add", Object.class);
        add.invoke(integers,"hello");

        System.out.println(integers);
    }
}


上一篇:codeforces CF986C AND Graph 建圖 dfs


下一篇:2020 BIT冬训-模拟与暴力 J - The Artful Expedient CodeForces - 869A