public class TestModel1 {
private String name;
private String age;
private String phone;
}
public class RunModelTest {
private static void getClassInfo(TestModel1 object) {
Field[] fields = object.getClass().getDeclaredFields();
Field field = null;
try {
for (Field key :fields) {
field = object.getClass().getDeclaredField(key.getName());
// 私有属性允许访问
field.setAccessible(true);
// 根据key去读取value
System.out.println("{key:" + key.getName()+",value:"+field.get(object)+"}");
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestModel1 testModel1 = new TestModel1();
testModel1.setAge("23");
testModel1.setName("liubingzhe");
testModel1.setPhone("6666666666");
getClassInfo(testModel1);
}
}
参考:https://www.jianshu.com/p/9be58ee20dee



