Java通过属性名称获取属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18044978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java get property value by property name
提问by kassie
Is it possible in Java to get class property value by its name? for example, i have class like
在 Java 中是否可以通过名称获取类属性值?例如,我有这样的课程
public class Test {
private String field;
public String getField() {...}
public void setField() {...}
}
and another class with Map
和另一个带有 Map 的类
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); ?
}
}
采纳答案by Dan94
You can use some of the Libraries that offer property-based access. I think the most known and used is beanutils. You can find one good example of the beanutils "in action" here. Some sample code:
您可以使用一些提供基于属性的访问的库。我认为最著名和最常用的是beanutils。您可以在此处找到 beanutils 的一个“运行中”示例。一些示例代码:
A someBean = new A();
// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);
// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny");
回答by bjc2406
Yes. You can replace the commented out line with t.getClass().getField(map.get(key)).get(t). which will retrieve the value of the field on t.
是的。您可以用 t.getClass().getField(map.get(key)).get(t) 替换注释掉的行。这将检索 t 上的字段值。
回答by Lokesh
You can also go for Properties.java class : http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html
你也可以去 Properties.java 类:http: //docs.oracle.com/javase/6/docs/api/java/util/Properties.html
It does same job.
它做同样的工作。
回答by Jesse van Bekkum
The question is how often do the properties change? Are the constants, or will it depend on the situation?
问题是属性多久更改一次?是常数,还是视情况而定?
If it is the latter case, which it often is, you want the properties in an external file. The standard java properties api is great for that: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html.
如果是后一种情况(通常是这种情况),您需要外部文件中的属性。标准的 java 属性 api 非常适合:http: //docs.oracle.com/javase/7/docs/api/java/util/Properties.html。
If you use a framework like spring, it will also come with standard ways to deal with properties. Look in their documentation.
如果你使用像 spring 这样的框架,它也会带有处理属性的标准方法。查看他们的文档。
回答by Danny Salvadori
bjc2406's answer works fine as long as the field(s) in question are accessible:
只要有问题的字段可访问,bjc2406 的答案就可以正常工作:
t.getClass().getField(map.get(key)).get(t)
t.getClass().getField(map.get(key)).get(t)
If you can't reasonably make it public, reflection and other field access APIs should get the job done: How do I read a private field in Java?
如果您不能合理地将其公开,反射和其他字段访问 API 应该可以完成工作:如何在 Java 中读取私有字段?
回答by fyrkov
Apart from String org.apache.commons.beanutils.BeanUtils.getProperty(object, propertyName)
there is Object org.apache.commons.beanutils.PropertyUtils#getProperty(object, propertyName)
which does not turn value into String.
除了String org.apache.commons.beanutils.BeanUtils.getProperty(object, propertyName)
有Object org.apache.commons.beanutils.PropertyUtils#getProperty(object, propertyName)
不把值转换为字符串。
This might be helpful to preserve integer, decimal and boolean types.
这可能有助于保留整数、十进制和布尔类型。
回答by Gawel
This solution work :
此解决方案工作:
// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny");
This solution variante preserve object's type
此解决方案变体保留对象的类型
Object a = BeanUtilsBean.getInstance().getPropertyUtils().getNestedProperty(someBean, "name");
if(a instanceof Boolean) {
...
}