为什么基类(不实现Serializable)如果其子类实现Serializable,则应该没有参数构造函数? [英] Why base class(not implementing Serializable) should have no argument constructor if its subclass implements Serializable?

查看:484
本文介绍了为什么基类(不实现Serializable)如果其子类实现Serializable,则应该没有参数构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读接口 Serializable 的文档,其中我找到以下行:

I am reading the docs of interface Serializable,in which i find the following lines:


为了允许序列化非可序列化类的子类型,子类型可能负责保存和恢复超类型的public,protected和(如果可访问)包字段的状态。只有当它扩展的类具有可访问的no-arg构造函数来初始化类的状态时,子类型才可以承担此责任。如果不是这种情况,则声明类Serializable是错误的。错误将在运行时检测到。

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.

但是基类的no-arg构造函数在恢复状态时的作用是什么?对象?

But what is the role of no-arg constructor of base class in restoring the state of the object?

推荐答案

当您尝试反序列化可序列化对象时,该机制必须创建该对象的空实例,并填写成员,将对象恢复到序列化时的状态。首次构造对象时,将调用可序列化对象的构造函数,但在反序列化期间不会调用构造函数,因为从技术上讲,您不构造对象,而是将其重构为前一个状态。预期构造和子序列操作的任何影响都已经包含在对象状态中。

When you attempt to deserialize an serializable object, the mechanism must create an empty instance of the object, and fill in the members, to restore the object to the state it was when serialized. A constructor of a serializable object would have been called when the object was first constructed, but constructors are NOT called during the deserialization because, technically, you are not constructing the object, instead reconstituting it to the former state. Any effects of construction and subsequence manipulation are expected to already be incorporated into the object state.

每当构造任何类的对象时,Java必须调用超级构造函数class,super-super-class等。你可以使用 super(...)指定超类的特定构造函数,或者如果你没有指定超级构造函数,将使用默认构造函数。无论如何,都构造了root的所有类。

Whenever you construct an object of any class, Java must call a constructor of the super class, and the super-super-class, etc. You can specify a specific constructor for the super class by using super(...) or if you don't specify a super constructor, the default constructor will be used. One way or another, all classes to the root are constructed.

对可归类化对象进行反序列化不会导致构造函数调用,但是当存在不可序列化的超类时, (即扩展具有可序列化类的非可序列化类)然后该类不期望被反序列化,并且它没有用于存储/恢​​复其成员的机制。如果超类不可序列化,则反序列化机制需要调用零参数构造函数以确保重构的对象实例正确初始化。

Deserialization of serlializable objects do not cause constructor invocation, but when there is a super class that is not serializable, (that is you extend a non-serializable class with a serializable class) then that class is not expecting to be deserialized, and it has no mechanism for storing/restoring its members. If the super class is not serializable, the deserialization mechanism needs to call the zero-argument constructor to make sure that the reconstituted object instance is initialized correctly.

如果您未能指定一个零参数构造函数,在您第一次尝试反序列化该类的对象之前,反序列化代码不会警告您此问题。编译时没有警告。

If you fail to specify a zero-argument constructor, the deserialization code will not warn you of this problem until your first attempt to deserialize an object of that class. There is no warning at compile time.

此外,您的可序列化子类必须负责存储/恢复来自非序列化超类的任何成员值。

Furthermore, your serializable subclass must take responsibility for storing/restoring any member values from the non-serializable super class.

这篇关于为什么基类(不实现Serializable)如果其子类实现Serializable,则应该没有参数构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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