我現在有這個問題一段時間了,並且設法避免它到現在。基本上我有一個課程,用一個返回這個信息的getter來存儲高爾夫球場每個洞的信息。在另一個類中,當用戶從洞1移動到洞2(當前存儲爲整數)時,我希望訪問洞2的getter方法,但不知道如何完成。Java - 使用動態名稱變體訪問類方法
代碼:
課程信息類:
//Hole information
private static String[] hole1 = {"name", "Distance", "Par"};
private static String[] hole2 = {"name", "Distance", "Par"};
public static String[] getHole1(){
return hole1;
}
public static String[] getHole2(){
return hole2;
}
主類:
String[] holeInfo;
int currentHole = 1;
//How I call hole1 info just now
//I just access the elements of the array as needed then
holeCoOrds = course.getHole1();
//User starts next hole (this will be the case for all 18 holes)
currentHole++
//Call next hole information getter method here
//Something along the lines of;
holeCoOrds = course.getHole(currentHole)();
任何幫助將不勝感激,我一直在貨架我的大腦有一段時間了,但沒有成功。
在此先感謝。
我不敢相信我沒有想到這一點,在等待期過後,將設定爲接受的答案。再次感謝 :) – Seatter