Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在 Javascript 中看到过,
var obj = { } function addMember(memberName,value) { obj[memberName] = value; }
IE。对象可以在对象/类声明中拥有名称未知的成员。那么在 Java 中是否有类似的方法,我可以将新成员添加到名称未知的对象中?
你可以用地图做到这一点。
Map<String, Object> map = new HashMap<>(); map.put(memberName, value);