62,614
社区成员
发帖
与我相关
我的任务
分享
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Aloha {
public void print() {
}
public void step1(String[] args) {
System.out.println("step1(): " + Arrays.toString(args));
}
public void step2(String[] args) {
System.out.println("step2(): " + Arrays.toString(args));
}
public void step3(String[] args) {
System.out.println("step3(): " + Arrays.toString(args));
}
public static void test(String argsString) throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
String[] args = argsString.split(",");
Method method = Aloha.class.getDeclaredMethod("step" + args.length, String[].class);
method.invoke(new Aloha(), new Object[] {args});
}
public static void main(String[] args) throws Exception {
test("1");
test("1, 2");
test("1, 2, 3");
}
}