像之前回答的那样,你应该使用:
Object value = field.get(objectInstance);
有时更喜欢的另一种方法是动态调用getter。示例代码:
public static Object runGetter(Field field, basevalidationObject o){ // MZ: Find the correct method for (Method method : o.getMethods()) { if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3))) { if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) { // MZ: Method found, run it try { return method.invoke(o); } catch (IllegalAccessException e) { Logger.fatal("Could not determine method: " + method.getName()); } catch (InvocationTargetException e) { Logger.fatal("Could not determine method: " + method.getName()); } } } } return null;}还应注意,当你的类继承自另一个类时,你需要递归确定Field。例如,获取给定类的所有字段;
for (Class<?> c = someClass; c != null; c = c.getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field classField : fields) { result.add(classField); } }


