有个类 Person 未实现Serializable接口,且又不能修改Person类
现在已经拿到Person的实例对象,
求教有什么方法让这个对象,能够序列化?
求大神指导!
------解决方案--------------------
没有实现Serializable接口的话,你只能自己去完成这个动作了.
你可以试试利用反射去读取实例中的字段,然后不管你是保存成XML还是JSON..还是二进制自定义格式都可以了.
------解决方案--------------------
你可以重新定义个新类,继承Person并且实现Serializable接口就可以了,之后使用新类就可以了,功能一点都不影响
例如:
public class Person_Serible implements Serializable{}
然后使用Person_Serible就可以了
------解决方案--------------------
若类 Person 未实现Serializable接口,可以肯定的说无法序列化Person的实例对象;
不过你可以自己定义一个类实现Serializable接口, 并把新类的字段定义成和Person的实例字段完全一样,这样先序列化新类的实例,反序列化时再赋值给Person实例;
示例:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class SerializableDemo {
public static void main(String[] args) throws Exception {
Person pers = new Person();
pers.setName("Lee");
pers.setAge(20);
pers.setBirthday(new Date());
PersonSerializable perSeri = new PersonSerializable();
perSeri.copy(pers);
ByteArrayOutputStream bou = new ByteArrayOutputStream();
ObjectOutputStream oou = new ObjectOutputStream(bou);
oou.writeObject(perSeri);
ByteArrayInputStream bin = new ByteArrayInputStream(bou.toByteArray());
ObjectInputStream oin = new ObjectInputStream(bin);
PersonSerializable perSeriClone = (PersonSerializable)oin.readObject();
System.out.println(perSeriClone.getName());
System.out.println(perSeriClone.getAge());
System.out.println(perSeriClone.getBirthday());
System.out.println("======================");
Person persClone = perSeriClone.copyToPerson();
System.out.println(persClone.getName());
System.out.println(persClone.getAge());
System.out.println(persClone.getBirthday());
}
}
class PersonSerializable implements Serializable {
private static final long serialVersionUID = -8564152324839732089L;
private String name;
private int age;
private Date birthday;
public void copy(Person person){
if (person == null) return;
this.setName(person.getName());
this.setBirthday(person.getBirthday());
this.setAge(person.getAge());
}
public Person copyToPerson(){
Person person = new Person();
person.setName(this.getName());
person.setBirthday(this.getBirthday());
person.setAge(this.getAge());
return person;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
class Person {
private String name;
private int age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
------解决方案--------------------
第二种解法,OK了!!!
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.io.IOException;
import java.io.Serializable;
public class Person_Serible extends Person implements Serializable {
private static final long serialVersionUID = 1L;
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();// 先序列化对象
out.writeUTF(super.getName());// 再序列化父类的域
out.writeInt(super.getAge());// 再序列化父类的域
}
private void readObject(java.io.ObjectInputStream in) throws IOException,