java 序列化会保存超类字段吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16442802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will serialization save the superclass fields?
提问by Danijel
My subclass implements Serializable, but my superclass does not.
我的子类实现了 Serializable,但我的超类没有。
Both subclass and superclass contain variables that need to be saved as part of the state of the subclass.
子类和超类都包含需要作为子类状态的一部分保存的变量。
Will serialization save the superclass fields?
序列化会保存超类字段吗?
回答by Juned Ahsan
A superclass fields cannot be serialized if it is not Serializable.Here is a summary of some rules of Java serialization:
一个超类的字段如果不是 Serializable 就不能被序列化。 这里总结了一些 Java 序列化的规则:
An object is serializable only if its class or its superclass implements the
Serializable
(orExternalizable
) interface.An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the firstsuperclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a
java.io.InvalidClassException
in runtime.The no-arg contructor of every non-serializable superclass will run when an object is deserialized. However, the deserialized objects? constructor does not run when it is deserialized.
The class must be visible at the point of serialization.
All primitive types are serializable.
Transient fields (with transient modifier) are NOT serialized, (i.e., not saved or restored). A class that implements Serializablemust mark -transient fields of classes that do not support serialization (e.g., a file stream).
Static fields (with static modifier) are Not serialized.
If member variables of a serializable object reference to a non-serializable object, the code will compile but a RumtimeExceptionwill be thrown.
只有当一个对象的类或它的超类实现了
Serializable
(或Externalizable
)接口时,它才是可序列化的。一个对象是可序列化的(它本身实现了 Serializable 接口),即使它的超类不是。但是,可序列化类层次结构中的第一个超类,没有实现 Serializable 接口,必须有一个无参数构造函数。如果违反了这一点, readObject() 将
java.io.InvalidClassException
在运行时产生一个。当对象被反序列化时,每个不可序列化的超类的无参数构造函数都会运行。但是,反序列化的对象呢?构造函数在反序列化时不会运行。
该类必须在序列化点可见。
所有原始类型都是可序列化的。
瞬态字段(带有瞬态修饰符)未序列化,(即,未保存或恢复)。实现 Serializable 的类必须标记不支持序列化的类的瞬态字段(例如,文件流)。
静态字段(带有静态修饰符)未序列化。
如果可序列化对象的成员变量引用了不可序列化对象,则代码将编译但会抛出 RumtimeException。
回答by Michal Borek
If superclass is not Serializable
fields won't be serialized. What is more you need to have no-args constructor in superclass.
如果超类不是Serializable
字段,则不会序列化。更重要的是,您需要在超类中有无参数构造函数。
As documentation says:
正如文档所说:
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.
在反序列化期间,不可序列化类的字段将使用类的公共或受保护的无参数构造函数进行初始化。可序列化的子类必须可以访问无参数构造函数。