invokeMethod(dd, "Nums",???);如何传参数
public class DumpMethods {
//写一些方法
private Integer Nums(){
int j=5;
int k=3;
return j+k;
}
// 执行该对象的方法
public static Object invokeMethod(Object owner,String methodName,Object [] args)
throws Exception{
Class ownerClass=owner.getClass();
Class [] argsClass=new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
}
public static void main(String []args){
DumpMethods dd=new DumpMethods(str);
/////////////////////////////
invokeMethod(dd, "Nums",???);
}
请问这里要invokeMethod(dd, "Nums",???);如何传参? 本人新手,求高手指点!!
}
不知道我有没有理解错,感觉你是想在dd上做反射,???只是入参:
import java.lang.reflect.Method;
public class DumpMethods {
// 写一些方法
public static Integer Nums(Integer j, Integer k) {
System.out.println("Nums executed");
return j + k;
}
// 执行该对象的方法
public static void invokeMethod(Object owner, String methodName,
Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();