如果忘记一个Object的type如何取出Field中的values
public class People {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws Exception {
People beibei = new People();
beibei.setName("beibei");
beibei.setAge(23);
Field[] fs = beibei.getClass().getDeclaredFields();
for(Field f: fs){
String methodName = "get"+upChar(f.getName());
System.out.println(
beibei.getClass().getMethod(methodName, null).invoke(beibei, null)
);
}
}
private static String upChar(String str){
char[] str2 = str.toCharArray();
if(str2[0]>='a'&&str2[0]<'z') {
str2[0]=(char)(str2[0]-32);
}
return new String(str2);
}
}