我想调用使用反射的方法。使用反射的Java调用方法
我调用的方法不是静态的,而是在我调用它的同一个类中。
的简化版本我的代码:
public class Test {
public static void main(String[] args) {
Test instance = new Test();
if (args.length > 0) {
instance.doWork(args[0]);
}
}
private void doWork(String methodName) {
Method method;
try {
method = this.getClass().getMethod(methodName);
method.invoke(this);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
[...]
}
}
private void MethodOne() { ... };
private void MethodTwo() { ... };
[...]
private void MethodTwenty() { ... };
}
我所得到的是一个java.lang.NoSuchMethodException: correct.package.and.class.MethodTwo()
尽管包/类/方法存在。
谁能告诉我什么,我做错了什么?
什么是'ARG [0]'?尝试打印出来。 – Sweeper