ohmydengzi
3/8/2019 - 6:34 AM

javabean遍历

public class User{
 
    private String name;
 
    private Integer age;
 
    //省略get set
}

User user = new User();
user.setName("Any");
user.setAge(1);
 
Class cls = user.getClass();
Field[] fields = cls.getDeclaredFields();
 
for (int i = 0; i < fields.length; i++) {
	Field field = fields[i];
	field.setAccessible(true);
        System.out.println("属性名:" + field .getName() + " 属性值:" + field .get(user));
 }