简体   繁体   English

反序列化时,父类(未实现序列化接口)构造函数被调用两次

[英]While deserilization, Parent class(Not implementing the Serialization interface) constructor are called twice

I have defined two classed as shown below我定义了两个类,如下所示

public class D{

    private String name;

    public D(){
        System.out.println("class D : constructor called !!");
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
}

public class E extends D implements Serializable{

    private String name;

    public E(){
        System.out.println("class E : constructor called !!");
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
}

While doing the Serialization it is working as expected, code for the same is :-在进行序列化时,它按预期工作,相同的代码是:-

public class TestSerialization{

    private static final String FILE_NAME = "E.ser";

    public static void main(String[] args){

        E e = new E();
        e.setName("New Name added");
        writeObject(e);
        E finalE = readObject();
        System.out.println(finalE.getName());

    }

    private static E readObject(){
        E e = null;
        try{
            FileInputStream fis = new FileInputStream(FILE_NAME);
            ObjectInputStream inputStream = new ObjectInputStream(fis);
            e =(E)inputStream.readObject();
            inputStream.close();
        }catch (FileNotFoundException ex){
            System.out.println("Exception : "+ex.getMessage());
        }catch (IOException ex){
            System.out.println("Exception : "+ex.getMessage());
        }catch (ClassNotFoundException ex){
            System.out.println("Exception : "+ex.getMessage());
        }
        return e;
    }

    private static void writeObject(E e){
        try{
            FileOutputStream fos = new FileOutputStream(FILE_NAME);
            ObjectOutputStream outputStream = new ObjectOutputStream(fos);
            outputStream.writeObject(e);
            outputStream.flush();
            outputStream.close();
        }catch (FileNotFoundException ex){
            System.out.println("Exception : "+ex.getMessage());
        }catch (IOException ex){
            System.out.println("Exception : "+ex.getMessage());
        }
    }
}   

During Serialization Constructor chaining is coming correct :- Output在序列化构造函数链接期间是正确的:- 输出

class D : constructor called !!
class E : constructor called !!

but during deserialization it is bit confusing:- Output但在反序列化过程中有点混乱:-输出

class D : constructor called !!
class E : constructor called !!
class D : constructor called !!

Why class D constructor is called again ?为什么再次调用 D 类构造函数?

During Serialization Constructor chaining is coming correct :- Output

class D : constructor called !!
class E : constructor called !!

I think you are confused or have misunderstood the concept我认为你混淆或误解了这个概念

If you change your code in main class as如果您将主类中的代码更改为

public static void main(String[] args){

    E e = new E();
    e.setName("New Name added");

    System.out.println("Serialization Started");
    writeObject(e);

    System.out.println("Deserialization Started");
    E finalE = readObject();
    System.out.println(finalE.getName());

}

Your output will be as你的输出将是

class D : constructor called !!
class E : constructor called !!
Serialization Started
Deserialization Started
class D : constructor called !!
New Name added

So you can see here Constructor of E and D are not called at the time of serialization but it is called while you are creating object e.所以你可以在这里看到 E 和 D 的构造函数在序列化时没有被调用,而是在你创建对象 e 时被调用。

While deserialization of your object, its constructors doesn't called, but default constructor of its parent will be called.在反序列化对象时,不会调用其构造函数,但会调用其父对象的默认构造函数。

So it is mandatory to have a standard constructor without parameters of all the parent class or else it will throw exception java.io.InvalidClassException所以必须有一个没有所有父类参数的标准构造函数,否则它会抛出异常 java.io.InvalidClassException

You are double-counting the two messages that are printed when you construct the E .您正在重复计算构建E时打印的两条消息。 That's not part of deserialization.这不是反序列化的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM