最病 A Java Object Serialization --How a Objects to be saved in the stream may support either the Serializable or the Exfernalizable interface n For Serializable objects, the stream includes sufficient information to restore the fields in the stream to a compatible version of the class n For Externalizable objects, the class is solely responsible for the external format of its contents. Institute of Computer Software 2021/1/28 Nanjing University
Java Object Serialization -- How Objects to be saved in the stream may support either the Serializable or the Externalizable interface. For Serializable objects, the stream includes sufficient information to restore the fields in the stream to a compatible version of the class. For Externalizable objects, the class is solely responsible for the external format of its contents. 2021/1/28 Institute of Computer Software Nanjing University 13
最病 Th e serializable terface a public interface java. io Serializable i o A serializable class must do the following a Implement the java. io Serializable interface a ldentify the fields that should be serializable a Have access to the no-arg constructor of its first nonserialIzable superclass Institute of Computer Software 2021/1/28 Nanjing University
The Serializable Interface public interface java.io.Serializable { }; A Serializable class must do the following: Implement the java.io.Serializable interface Identify the fields that should be serializable Have access to the no-arg constructor of its first nonserializable superclass 2021/1/28 Institute of Computer Software Nanjing University 14
最病 Th e serializable terface a The class can optionally define the following methods: a write Object(Object Stream) a readobject(ObjectInputStream) a write Replace a readResolveo 思考:如果一个可序列化的类实现了以上四个方 法,那么在序列化和反序列化的过程中,这几个 方法的调用次序如何? Institute of Computer Software 2021/1/28 Nanjing University
The Serializable Interface The class can optionally define the following methods: writeObject (ObjectOutputStream) readObject (ObjectInputStream) writeReplace () readResolve () 2021/1/28 Institute of Computer Software Nanjing University 15 思考:如果一个可序列化的类实现了以上四个方 法,那么在序列化和反序列化的过程中,这几个 方法的调用次序如何?
最病 A The Externalizable Interface public interface Externalizable extends serializable public void write External(object Output out throws eXCeption public void read External(object Input in) throws lOException ava.lang. ClassNotFound Exception Institute of Computer Software 2021/1/28 Nanjing University
The Externalizable Interface public interface Externalizable extends Serializable { public void writeExternal(ObjectOutput out) throws IOException; public void readExternal(ObjectInput in) throws IOException, java.lang.ClassNotFoundException; } 2021/1/28 Institute of Computer Software Nanjing University 16
最病 A The Externalizable Interface a The class of an externalizable object must do the following: a Implement the java. io Externalizable interface a Implement a write External method to save the state of the object a Implement a readExternal method to read the data written by the write External method from the stream and restore the state of the object a Have the write External and readExternal methods be solely responsible for the format, if an externally defined format is written a Have a public no-arg constructor Institute of Computer Software 2021/1/28 Nanjing University
The Externalizable Interface The class of an Externalizable object must do the following: Implement the java.io.Externalizable interface Implement a writeExternal method to save the state of the object Implement a readExternal method to read the data written by the writeExternal method from the stream and restore the state of the object Have the writeExternal and readExternal methods be solely responsible for the format, if an externally defined format is written Have a public no-arg constructor 2021/1/28 Institute of Computer Software Nanjing University 17