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

通过反射我们可以获取到一个类的属性和方法,但获取父类的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]


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值