[Java] 纯文本查看 复制代码
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Exec3 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ArrayList<Integer> arraylist=new ArrayList<Integer>();
String str="i am string";
Integer i=3;
Method m1=arraylist.getClass().getMethod("add",Object.class);
arraylist.add(i);
m1.invoke(arraylist, i);
//arraylist.add(str);
m1.invoke(arraylist, str);
for(Object o:arraylist){
System.out.println(o);
}
Method m2=arraylist.getClass().getMethod("add",Object.class);
m2.invoke(arraylist, 5,i);
m2.invoke(arraylist, 6,str);
for(Object o:arraylist){
System.out.println(o);
}
}
}