通过反射来实现
ArrayList<Integer> list = new ArrayList<Integer>();
Class l = list.getClass();
Method m = l.getMethod("add", Object.class);//这里必须是这两个类的共同父类
System.out.println(m.getName());
m.invoke(list, 12);
m.invoke(list, "zhangsan");
System.out.println(list.get(0)+"*****" + list.get(1)); |