/**
* 创建List<Integer></>集合,获取反射对象
* 使用class的method方法,找到集合的add方法
* 使用method类的invoke方法,添加字符串到集合中
*
* @param args
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
Class<? extends ArrayList> clazz = list.getClass();
Method add = clazz.getMethod("add", Object.class);
add.invoke(list, "测试");
for (Object item : list) {
System.out.println("item = " + item);
}
}