相关疑难解决方法(0)

当只有子类实现可序列化时,序列化如何工作

只有子类实现了Serializable接口.

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); …
Run Code Online (Sandbox Code Playgroud)

java inheritance serialization deserialization notserializableexception

23
推荐指数
2
解决办法
3万
查看次数