你可以做这样的事情。您可以使此代码更加通用,以便可以使用它来循环访问字段:
Class aClass = f.getClass();
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class; // get the actual param type
String methodName = "set" + fieldName; // fieldName String
Method m = null;
try {
m = aClass.getMethod(methodName, paramTypes);
} catch (NoSuchMethodException nsme) {
nsme.printStackTrace();
}
try {
String result = (String) m.invoke(f, fieldValue); // field value
System.out.println(result);
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}