我真的不確定我是否用正確的名稱來稱呼這些東西,所以很抱歉。在Java中通過字符串獲取類成員
我這有一個類文件:
class eSpecies {
protected String name;
protected String file;
protected int hp;
protected int atk;
protected int exp;
protected int type;
public int kill;
protected String flavour;
eSpecies(String name, String file, int hp,int atk, int exp, int type, int kill, String flavour){
this.name=name;
this.file=file;
this.hp=hp;
this.atk=atk;
this.exp=exp;
this.type=type;
this.kill=kill;
this.flavour=flavour;
}
}
public class enemyDB{
static eSpecies lsp = new eSpecies("Lesser Shelled Pincher", "lsp", 500, 30, 50, 1, 0, "this is a small bug");
static eSpecies gsp = new eSpecies("Greater Shelled Pincher", "gsp", 50000, 50, 500, 1, 0, "this is a small bug");
}
,並已參考它在另一個像這樣:
eSpecies e = enemyDB.lsp;
我想知道如果有可能基本上做到這一點:
String type = "lsp";
eSpecies e = enemyDB.type;
顯然,這是不行的,但我希望這個想法是明確enough-通過使用得到一個類對象字符串來獲取其名稱的一部分。謝謝!
聽起來像一個地圖將做你想做的事情https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html –
最後使用一張地圖,並得到它完美地工作 - 謝謝!! – Saiklex