Java 按属性名称获取属性值
2022-09-02 03:48:55
在Java中,是否可以通过其名称获取类属性值?例如,我有类像
public class Test {
private String field;
public String getField() {...}
public void setField() {...}
}
和另一个带有地图的类
public class Main {
private static final Map<String, Long> map = new HashMap<String, Long>();
static {
map.put("field", new Long(1));
}
public void doSth() {
Set<String> keys = map.keySet();
Test t = new Test();
for (String key : keys) {
//t.getPropertyValueByName(key); ?
}
}