本帖最后由 黄小贝 于 2012-10-4 17:00 编辑
亲爱的~你犯了两个错误~~
第一个错误你应该懂~~粗心了~
第二个错误~~请看getMethod的函数声明~
public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
// be very careful not to change the stack depth of this
// checkMemberAccess call for security reasons
// see java.lang.SecurityManager.checkMemberAccess
checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
Method method = getMethod0(name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
}
return method;
}
更正后
import java.lang.reflect.Method;
import java.util.ArrayList;
public class PutString {
public static void main(String[] args) throws Exception{
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(123);
arraylist.add(234);
Class clazz = ArrayList.class;//错误1
Object obj = clazz.newInstance();
Method methods = clazz.getMethod("add",Object.class);//错误2
methods.invoke(obj, "abcdefg");
for(Integer a : arraylist){
System.out.println(a);
}
}
}
//这里想把abcdefg放进集合中
|