我正在开发一个测验,我想使用问题编号作为变量调用以下方法:
private void question2() {
....
}
所以不要这样叫:
question2();
我想这样称呼:
question+number(); (where number is defined as string "2")
我试过了:
Method method = MyClassName.class.getDeclaredMethod("question" + number);
method.invoke();
但是得到错误:
The method invoke(Object, Object...) in the type Method is not applicable for the arguments ()
答案 0 :(得分:0)
void question(int questionNumber)
{
switch(questionNumber)
case 1: // Do what you want to do for question 1
break;
case 2: // Do what you want to do for question 2
break;
}
这是解决问题的正确方法。