我有一个返回对象
toString()
方法显示我的对象有两个 BigDecimal
属性。但我不知道如何在代码中获取它们?
我的函数使用 hibernate 从查询中获取结果是:
public Object executeQuery(final String sql) {
final Query query = getSessionFactory().getCurrentSession().createSQLQuery(sql);
return query.list().get(0);
}
谢谢。
-- 附加信息:
obj.getClass().getDeclaredFields(); // empty array []
obj.getClass().getName(); // [Ljava.lang.Object;
final BigDecimal b = (BigDecimal) obj[0]; //Compilation error: The type of the expression must be an array type but it resolved to Object
最佳答案
obj.getClass().getDeclaredFields()
可以帮助您。一般学习反射API。如果你反对 bean,你也可以使用 Jackarta BeanUtils。
关于java - 如何在Java中获取对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14384937/