Java 通过反射获取类和父类的属性和属性值

peterson · 2019-12-25 10:25:44 · 热度: 45

通过反射我们可以获取到一个类的属性和方法,但获取父类的private的属性却不可以直接获取,需要递归调用superclass来获取属性

SuperClass.java

public class SuperClass {

    private String name;
    private String age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}

SubClass.java

public class SubClass extends SuperClass {
    private String sid;

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

}

SubSubClass.java

public class SubSubClass extends SubClass {
    private String grade;

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

}

保存属性和属性值

SensorData.java

public class SensorData {
    private String  sensorId;
    private Object sensorValue;

    public String getSensorId() {
        return sensorId;
    }

    public void setSensorId(String sensorId) {
        this.sensorId = sensorId;
    }

    public Object getSensorValue() {
        return sensorValue;
    }

    public void setSensorValue(Object sensorValue) {
        this.sensorValue = sensorValue;
    }

    @Override
    public String toString() {
        return "SensorData [sensorId=" + sensorId + ", sensorValue=" + sensorValue + "]";
    }


}

TestRe.java 测试类

public class TestRe {

    public static void main(String[] args) {
        SuperClass cla =new SubSubClass();
        //SuperClass cla =new SubClass();

        List<SensorData>  s = sensorDataList(cla);
        for(SensorData ss :s){
            System.out.println(ss);
        }
    }

    /**
     * 通过反射获取各个属性名称和属性值封装成类
     * 
     * @param sensorDataDto
     * @return
     */
    public static List<SensorData> sensorDataList(Object sensorDataDto) {
        List<SensorData> sensorDatas = new ArrayList<SensorData>();
        Class<?> clazz = sensorDataDto.getClass();
        try {
            exceClass(sensorDataDto, sensorDatas, clazz);
        } catch (Exception e) {

        }
        return sensorDatas;
    }

    private static void exceClass(Object sensorDataDto, List<SensorData> sensorDatas, Class<?> clazz) throws Exception {
        if (clazz != Object.class) {
            System.out.println(clazz);
            returnclassF(sensorDataDto, sensorDatas, clazz);
            Class<?> clazzs = clazz.getSuperclass();
            exceClass(sensorDataDto, sensorDatas, clazzs);
        }
    }

    private static void returnclassF(Object sensorDataDto, List<SensorData> sensorDatas, Class<?> clazz) throws Exception {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            SensorData sensorData = new SensorData();
            sensorData.setSensorId(field.getName().toString());
            sensorData.setSensorValue(field.get(sensorDataDto));
            sensorDatas.add(sensorData);
        }
    }
}

通过

class com.ref.SubSubClass
class com.ref.SubClass
class com.ref.SuperClass
SensorData [sensorId=grade, sensorValue=null]
SensorData [sensorId=sid, sensorValue=null]
SensorData [sensorId=name, sensorValue=null]
SensorData [sensorId=age, sensorValue=null]
SensorData [sensorId=birthday, sensorValue=null]

通过多态可以获取不同类的属性和值

class com.ref.SubClass
class com.ref.SuperClass
SensorData [sensorId=sid, sensorValue=null]
SensorData [sensorId=name, sensorValue=null]
SensorData [sensorId=age, sensorValue=null]
SensorData [sensorId=birthday, sensorValue=null]

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册