java中实现⽤for循环动态创建变量或动态调⽤⽅法在⽹上了好久,好像没有很好的⽅法。看来以后的开发要尝试动态语⾔了。
折中的办法如下:
/**
*
* @author Administrator
*/
public class Test {
private String answer1 = "1";
private String answer2 = "2";java switch case string
private String answer3 = "3";
public static void main(String[] args) {
Test test = new Test();
for (int i = 0; i < 3; i++) {
String answer = "";
switch(i) {
case 1 : answer = Answer1(); break;
case 2 : answer = Answer2(); break;
case 3 : answer = Answer3(); break;
default : answer = "";
}
System.out.println("answer" + i + " : " + answer);
}
}
public String getAnswer1() {
return answer1;
}
public void setAnswer1(String answer1) {
this.answer1 = answer1;
}
public String getAnswer2() {
return answer2;
}
public void setAnswer2(String answer2) {
this.answer2 = answer2;
}
public String getAnswer3() {
return answer3;
}
public void setAnswer3(String answer3) {
this.answer3 = answer3;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论