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); } }
原文:https://www.cnblogs.com/pxy-1999/p/13173998.html