Talking about the sequence of serialization in J2SE encounters inheritance

xiaoxiao2021-03-06  21

When a parent class implements the Serializable interface, his subclasses will automatically implement serialization. This is verified below:

package Serial; import java.io.Serializable; public class SuperC implements Serializable {// parent class implements serialization int supervalue; public SuperC (int supervalue) {this.supervalue = supervalue;} public String toString () {return "supervalue : " supervalue;}} public class SubC extends SuperC {// subclass int subvalue; public SubC (int supervalue, int subvalue) {super (supervalue); this.subvalue = subvalue;} public String toString () {return super .toString () "sub:" subvalue;}} public class Test1 {public static void main (String [] args) {SubC subc = new SubC (100,200); FileInputStream in = null; FileOutputStream out = null; ObjectInputStream oin = NULL; ObjectOutputStream Oout = null; try = null; tryoutputstream ("test1.txt"); // Sub-class serialization Oout = New ObjectOutputStream (OUT); Oout.WriteObject (SUBC); Oout.Close (); Oout = NULL; IN = New fileinputStream ("test1.txt"); OIN = New ObjectInputStream (in); SUBC SUBC2 = (SUBC) Oin.ReadObject (); // Subclass Anti-sequence System.out.Println (SUBC2) ;} Catch (exception ex) {ex.printStackTrace ();} finally {... This is omitted}}} The result is as follows:

SuperValue: 100 Sub: 200 Visible subclasses successfully serialized / reverse sequence. How to make a child to achieve serialization seem to be a very simple thing, but sometimes we can not let the parent classes to implement the serializable interface, the reason is that the parent class is abstract (this does not matter), And the parent class cannot force each subclass to have serialized capabilities. In other words, the purpose of the parent design is to be inherited. To prepare a quite trouble for a parent class that does not implement the serializable interface. Java DOCS mentioned:

"To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime. "That is, to a not implement Serializable The parent class, writing a subclass that can serialize to do two things: 1. The parent class must have a uncommon constructor; its second, subclasses are responsible for serialization (reverse sequence) parent class. We remove the Superc's serializable interface and add the SERIALIZABLE interface to SUBC. After running: java.lang. error: unresolved Compilation ProBLEM: Serializable Cannot Be Resolved or is not a valid superinterface at serial.subc. (subc.java: 15) at serial.test1.main (Test1.java : 19) Exception in thread "main" is really like the DOCS, the parent class lacks a connectionless constructor is not. Next, according to the recommendations in DOCS, we rewrite this example:

转载请注明原文地址:https://www.9cbs.com/read-60453.html

New Post(0)