反射练习之越过泛型检查

public class ReflectTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ArrayList<Integer> arrayList = new ArrayList<>();

        Class<? extends ArrayList> aClass = arrayList.getClass();
        Method aClassMethod = aClass.getMethod("add", Object.class);
        aClassMethod.invoke(arrayList,"hello");
        aClassMethod.invoke(arrayList,"world");
        aClassMethod.invoke(arrayList,"java");
        System.out.println(arrayList);
    }
}

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/13173998.html
今日推荐