使用反射可以如下:
public class Test {
public void method1() {
System.out.println("method1() invoked");
}
public void method2() {
System.out.println("method2() invoked");
}
public void method3() {
System.out.println("method3() invoked");
}
public void invokeMethod(String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Test.class.getMethod(methodName,null).invoke(this,null);
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
new Test().invokeMethod("method2");
}
}
不过按你的要求不知道怎么做