拥有一个班级Employee
,其中包含员工Name
,EmployeeId
和serialized
字段。
现在我通过持久1000
课程的deserialization
来获得超过Employee
个对象。
现在在address
班级中添加了新字段Employee
。
如何将新字段address
添加到已经deserialized
个对象中?
注意:,不再执行任何deserialization
。
答案 0 :(得分:0)
您可以创建一个从Employee
扩展的包装类。
public class EmployeeWithAddr extends Employee {
private String address;
public EmployeeWithAddr(Employee employee) {
//initialize from employee
}
public EmployeeWithAddr(Employee employee, String address) {
//initialize from employee
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
然后,你获取反序列化的对象,将它传递给包装器构造函数et voila,你就拥有它。
恕我直言,不需要黑魔法来改变运行时类并动态地在对象上添加新字段。这很好。