当只有子类实现可序列化时,序列化如何工作 [英] How serialization works when only subclass implements serializable

查看:123
本文介绍了当只有子类实现可序列化时,序列化如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有子类已实现 Serializable 界面。

  import java.io. *; 

公共类NewClass1 {

private int i;
NewClass1(){
i = 10;
}
int getVal(){
return i;
}
void setVal(int i){
this.i = i;
}
}

类MyClass扩展NewClass1实现Serializable {

private String s;
private NewClass1 n;

MyClass(String s){
this.s = s;
setVal(20);
}

public String toString(){
return s ++ getVal();
}

public static void main(String args []){
MyClass m = new MyClass(Serial);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serial.txt));
oos.writeObject(m); //写当前状态
oos.flush();
oos.close();
System.out.print(m); //显示当前状态对象值
} catch(IOException e){
System.out.print(e);
}
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serial.txt));
MyClass o =(MyClass)ois.readObject(); //读取保存的对象
ois.close();
System.out.print(o); //显示保存的对象状态
} catch(例外e){
System.out.print(e);
}
}
}

有一件事,我注意到了这里是,父类没有序列化。然后,为什么不抛出 NotSerializableException 确实它显示以下



输出

 序列号20 
序列号10

此外,输出不同于序列化反序列化。我只是知道,这是因为父类没有实现 Serializable 。但是,如果有人解释我,在对象序列化和反序列化过程中会发生什么。它如何改变价值?我无法弄清楚,我也在我的程序中使用了评论。所以,如果我在任何时候都错了,请告诉我。

根据 Serializable javadoc


在反序列化期间,非字段-serializable类将使用
类的public或protected no-arg构造函数初始化

serializable的子类必须可以访问no-arg构造函数。可序列化子类的字段将从流中恢复


此外,仅当类正在被抛出时才会抛出序列化异常serialized不可序列化。有不可序列化的父母是好的(只要他们有一个no-arg构造函数)。对象本身不是Serializable,一切都扩展了它。
上面的引用也解释了为什么你得到值字段的不同值 - 设置了父类的no-arg构造函数,它将value字段设置为10 - 该字段属于(不可序列化的)父类所以它的值不会写入流中或从流中读取。


Only subclass has implemented Serializable interface.

import java.io.*;

public class NewClass1{

    private int i;
    NewClass1(){
    i=10;
    }
    int getVal() {
        return i;
    }
    void setVal(int i) {
        this.i=i;
    }
}

class MyClass extends NewClass1 implements Serializable{

    private String s;
    private NewClass1 n;

    MyClass(String s) {
        this.s = s;
        setVal(20);
    }

    public String toString() {
        return s + " " + getVal();
    }

    public static void main(String args[]) {
        MyClass m = new MyClass("Serial");
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serial.txt"));
            oos.writeObject(m); //writing current state
            oos.flush();
            oos.close();
            System.out.print(m); // display current state object value
        } catch (IOException e) {
            System.out.print(e);
        }
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("serial.txt"));
            MyClass o = (MyClass) ois.readObject(); // reading saved object
            ois.close();
            System.out.print(o); // display saved object state
        } catch (Exception e) {
            System.out.print(e);
        }
    }
}

One thing, which I noticed here is, parent class is not serialized. Then, why didn't it throw NotSerializableException indeed it is showing following

Output

Serial 20
Serial 10

Also, output differ from Serialization and De-serialization. I just only know, it is because of parent class has not implemented Serializable. But, If anyone explain me, what happens during object serialization and de-serialization. How it changes the value ? I'm not able to figure out, also I have used comment in my program. So, if I'm wrong at any point, please let me know.

解决方案

according to the Serializable javadoc

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

also, serialization exception is only thrown if the class being serialized is not serializable. having non-serializable parents is fine (as long as they have a no-arg constructor). Object itself isnt Serializable, and everything extends it. the quote above also explains why you get different values for the value field - the no-arg constructor for the parent class is set, which sets the value field to 10 - the field belongs to the (non-serializable) parent so its value isnt written to/read from the stream.

这篇关于当只有子类实现可序列化时,序列化如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆