阿波羅的戰(zhàn)車(chē)
2022-07-27 20:26:22
public class myClass { private Object myObject; public myClass() { myObject = new myObjectClass(); } private class myObjectClass { private int A = 1; private int B = 5; } public int getMyObjectValue(String id) { return myObject.[[[[id]]]]; }}我想得到這樣的myObject財(cái)產(chǎn):myClass.getMyObjectValue("A")在Javascript我們可以做myObject[id];我怎樣才能改變r(jià)eturn myObject.[[[[id]]]];它的工作?
1 回答

夢(mèng)里花落0921
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
有關(guān)反射的更多信息https://docs.oracle.com/javase/tutorial/reflect/
Object someObject = myObject();
for (Field field : someObject.getClass().getDeclaredFields()) {
field.setAccessible(true); // You might want to set modifier to public first.
Object value = field.get(someObject);
if (value != null) {
System.out.println(field.getName() + "=" + value);
}
}
添加回答
舉報(bào)
0/150
提交
取消