简体   繁体   English

如果基类(未实现可序列化)的子类实现可序列化,为什么基类(未实现可序列化)应该没有参数构造函数?

[英]Why base class(not implementing Serializable) should have no argument constructor if its subclass implements Serializable?

I am reading the docs of interface Serializable ,in which i find the following lines:我正在阅读接口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.如果不是这种情况,则声明类 Serializable 是错误的。 The error will be detected at runtime.该错误将在运行时检测到。

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.预期构造和子序列操作的任何影响都已被纳入对象状态。

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.无论何时构造任何类的对象,Java 都必须调用超类的构造函数,以及超超类等。您可以使用super(...)为超类指定特定的构造函数,或者如果您不要指定超级构造函数,将使用默认构造函数。 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.此外,您的可序列化子类必须负责存储/恢复来自不可序列化超类的任何成员值。

In case super class is not Serializable than to serialize the subclass's object we must implement serializable interface in subclass explicitly.如果超类不是 Serializable 而不是序列化子类的对象,我们必须在子类中显式实现可序列化接口。 In this case the superclass must have a no-argument constructor in it.在这种情况下,超类中必须有一个无参数的构造函数。

If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process.如果超类不是可序列化的,那么从超类继承的实例变量的所有值都将在反序列化过程中通过调用非可序列化超类的构造函数来初始化。

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

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