java反射
程序员文章站
2024-01-21 14:36:34
...
Class classType=object.getClass();
Field[] fields=classType.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field field=fields[i];
String fieldName=field.getName();
String getName = "get"+fieldName;
try {
Method getMethod=classType.getMethod(getName, new Class[]{});
Object value=getMethod.invoke(object, new Object[]{});
System.out.println(fieldName);
System.out.println(fieldName+" :"+value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
反射小记