Package tigers;
Import java.io. *;
public class Tiger3 {static class Sub extends Super {private String name; public Sub (int id, int uid, String name) {super (id, uid); this.name = name;} public String toString () {return "Tiger3 $ SUB: (" Super.Tostring () ", " Name ") ";} void writeObject (ObjectOutputStream OOS) THROWS IOEXCEPTION {System.out.println (" Sub.WriteObject () "); OOS. defaultWriteObject ();} private void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException {System.out.println ( "Sub.readObject ()"); ois.defaultReadObject ();}} static class Super implements Serializable {private int id; private transient int uid; public Super (int id, int uid) {this.id = id; this.uid = uid;} private static final long serialVersionUID = 1L; private void writeObject (ObjectOutputStream oos) throws IOException {System.out. Println ("Super.WriteObject ()"); oos.defaultWriteObject (); Oos.WriteInt (UID);} Private Void ReadObject (ObjectInputStream Ois) Throws IOException, ClassNotFoundException {System.out.println ( "Super.readObject ()"); ois.defaultReadObject (); uid = ois.readInt ();} public String toString () {return "Tiger3 $ Super :(" id "," UID ")";}} public static void main (string [] args) {tiger3.super sup = new Tiger3.super (55, 1000); tiger3.sub sub = new tiger3.sub (12 , 100, "sub"); try {ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ( "Tiger3 $ Super.tmp")); oos.writeObject (sup); ObjectInputStream ois = new ObjectInputStream (new FileInputStream ( "Tiger3 $ Super. TMP "));
Sup = (tiger3.super) Ois.ReadObject (); system.out.println (SUP); OOS = New ObjectOutputStream (New FileoutPutStream ("Tiger3 $ Sub.TMP")); OOS.WriteObject (SUB); OIS = New ObjectInputStream (new FileInputStream ( "Tiger3 $ Sub.tmp")); sub = (Tiger3.Sub) ois.readObject (); System.out.println (sub);} catch (IOException ioe) {} catch (ClassNotFoundException cnfe) {}}}
Super.writerObject () Super.ReadObject () Tiger3 $ Super: (55, 1000) Super.writerObject () SUB.WriteObject () Super.readObject () Sub.readObject () Tiger3 $ SUB: (Tiger3 $ SUPER: (12,100 ), SUB)
If you have modified the modeis of WriteObject () and readObject () in Super and Subs into public:
Tiger3 $ super: (55, 0) Tiger3 $ SUB: (Tiger3 $ Super: (12, 0), SUB)
Tiger3 $ super: (55, 0) Tiger3 $ SUB: (Tiger3 $ Super: (12, 0), SUB)
in conclusion:
First, if the super class contains the serialized Primitive type variable, the WriteObject () and readObject () method should be implemented, and the objectObject () and ObjectInputStream.defaultReAdObject () methods are called separately.
Second, WriteObject () and readobject () modifiers, return types, parameters must meet the regulations, that is, must be the following format:
Private void writeobject (ObjectOutputstream O) throws ...
Private Void ReadObject (ObjectInputStream O) throws ...
Otherwise the two methods will not be called.
Third, if the parent class implements the serializable interface, the subclass will automatically get sequential feature. And when serialized, the WriteObject () and readObject () of the parent class will be called. In other words, the parent class will automatically serialize before serialization.
/ * * Created on 2005-2-7 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * / package tigers; import java.io. *; / ** * @author bitan * * TODO to change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class Tiger4 {static class Super {private String name; public Super (String name) {this.name = name;} public String toString () {return "Tiger4 $ Super :(" name ")";}} static class Sub extends Super implements Serializable {private String name; Sub (String superName, String name ) {Super (supername); this.name = name;} public string toString () {return "Tiger4 $ SUB: (" super.tostring () "," name ")";}} public static void Main (String [] args) {sub subs ("super", "sub"); try {system.out.println ("Before serialization: Sub); ObjectOutputStream Oos = New ObjectOutputStream (New FileoutputStream "Tiger4 $ Sub.TMP")); OOS.WRI Teobject (Sub); System.out.Println ("Serialized:" Sub); ObjectInputStream Ois = New ObjectInputStream (New FileInputStream (New FileInputStream ("Tiger4 $ Sub.TMP")); SUB = (SUB) OisreadObject () System.out.Println ("Declines:" Sub);} catch (ooexception ie) {IOE.PrintStackTrace ();} catch (Cause.PrintStackTrace () {cnfe.printStackTrace ();}}} Beforetruction: Tiger4 $ SUB: (Tiger4 $ SUPER: (Super), Sub) Sequence: Tiger4 $ SUB: (Tiger4 $ SUPER: (Super), SUB) Java.InvalidClassexception: TiGERS.TIGER4 $ Sub; NO Valid constructor at java.io.objectStreamClass.