情境:一个子类实现了 Serializable 接口,java
解决:函数
public abstract class SuperC { int supervalue; public SuperC(int supervalue) { this.supervalue = supervalue; } public SuperC(){}//增长一个无参的constructor public String toString() { return "supervalue: "+supervalue; } } public class SubC extends SuperC implements Serializable { int subvalue; public SubC(int supervalue,int subvalue) { super(supervalue); this.subvalue=subvalue; } public String toString() { return super.toString()+" sub: "+subvalue; } private void writeObject(java.io.ObjectOutputStream out) throws IOException{ out.defaultWriteObject();//先序列化对象 out.writeInt(supervalue);//再序列化父类的域 } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{ in.defaultReadObject();//先反序列化对象 supervalue=in.readInt();//再反序列化父类的域 } }
Transient 关键字的做用是控制变量的序列化,this