所以我叫測試類:Java中,獲得一類的所有變量值
public class Test{
protected String name = "boy";
protected String mainAttack = "one";
protected String secAttack = "two";
protected String mainType"three";
protected String typeSpeak = "no spoken word in super class";
//Somehow put all the class variables in an Array of some sort
String[] allStrings = ??(all class' strings);
//(and if you feel challenged, put in ArrayList without type declared.
//So I could put in, not only Strings, but also ints etc.)
public void Tester(){
//Somehow loop through array(list) and print values (for-loop?)
}
}
正如你所看到的,我希望把所有的類變量在數組中的ArrayList(或類似的東西)自動。 接下來我想能夠遍歷數組並打印/獲取值。 最好使用增強型for循環。
使用getters和setter。 –
爲什麼不直接使用數組呢?爲什麼你需要單獨的變量? –
這是一個可怕的想法,千萬不要這樣設計你的課程。如果你只是爲了測試反射,那很好。 「反思」是關鍵詞。 –