Talk about serialization in J2SE (2)

xiaoxiao2021-03-06  45

Talk about serialization in J2SE (2)

Author: Favo yang

Favoyang@yahoo.com

When serialization encounters inheritance ...

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 {// The 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) {

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 {

OUT = New FileOutputStream ("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 (); // Sub-deserialization

System.out.println (SUBC2);

} catch (exception ex) {

EX.PrintStackTrace ();

} finally {

...

}

}

}

The results of the operation are as follows:

SuperValue: 100 SUB: 200

Visible sequential / reverse selecente is successful.

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 of sequencing to do two things:

First, the parent class must have a uncommon constructor;

Second, subclasses are responsible for serialization (reverse sequence) parental domain.

We remove the Superc's serializable interface and add the SERIALIZABLE interface to SUBC. Errors 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"

As mentioned in DOCS, the parent class lacks a connectionless constructor.

Next, according to the recommendations in DOCS, we rewrite this example:

Public Abstract Class Superc {

Int superValue;

Public superc (int supervalue) {

THIS.SUPERVALUE = SuperValue;

}

Public superc () {} // Add a uncommon constructor

Public string toString () {

Return "SuperValue:" SuperValue;

}

}

Public Class Subc Extends Superc IMPLEMENTS SERIALIZABLE {

Int Subvalue;

Public SUBC (int supervalue, int subvalue) {

SuperValue;

THIS.SUBVALUE = SubValue;

}

Public string toString () {

Return super.toString () "Sub:" SubValue;

}

Private void writeObject (java.io.ObjectOutputstream out)

THROWS IOEXCEPTION {

Out.defaultWriteObject (); // First serialized

Out.writeInt (SuperValue); // Sequence the field of the parent class

}

Private void readObject (java.io.objectInputStream in)

Throws ioException, classnotfoundexception {in.defaultReadObject (); // Pre-serialized object

SuperValue = in.readint (); // Reverse sequence-of-parent class domain

}

}

The operation results prove that this method is correct. Here we use the WriteObject / ReadObject method, if there is, if you exist, you will be called when serialization, instead of the default behavior (you must discuss later, you know so much first). When we serialize, we first call the DefaultWriteObject of ObjectOutputStream, which uses the default serialized behavior, then serialize the field of the parent class; the same time is the same.

Incident:

purpose

behavior

Write a subclass that enables sequentialization for a parent class that implements the Serializable interface

Subclasses will automatically realize serialization

For a parent class that does not implement the serializable interface, write a subclass that can be serialized

1. The parent class must have a uncommon constructor;

2, subclasses must first serialize themselves, then subclasses should be responsible for serialization of the parent class.

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

New Post(0)