如果我有一个类Base
,其中包含:
public static Tab thisTab = new TabClass(5200, "thisItem");
public final static Item thisItem = new SomeClass(par1, par2);
在TabClass
课程中,我希望能够从thisItem
访问某些信息,但TabClass
知道thisItem
的名称
public TabClass(int par1, String par2) {
int num;
Base.(Here is where i need the item that par2 is the name of).ID = num;
}
在查找ID的值时,我怎么能以某种方式获得par2
表示实际可用的字段?
答案 0 :(得分:0)
尝试java反射方法..
Item item = new Item();
Field field = Item.class.getDeclaredField(par2);
field.set(item, new Item(234));
你的Item类有构造函数
public Item(int ID){
this.ID = ID;
}