扩展类的 Java 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18417737/
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
Java Serialization for Extended Class
提问by rama
In java serialization class Mp3player extends ElectronicDevice implements Serializable in this code super class electronicdevice is not implemented serializable. here super class is also getting serialized. my understanding is super class is also gets serialized due to extends.let me know my understanding is correct or not.
在 java 序列化类 Mp3player extends ElectronicDevice implements Serializable 在这段代码中超类电子设备没有实现可序列化。这里的超类也正在序列化。我的理解是超类也由于扩展而被序列化。让我知道我的理解是否正确。
import java.io.*;
class ElectronicDevice {
ElectronicDevice()
{
System.out.print("ed ");
}
}
class Mp3player extends ElectronicDevice implements Serializable {
Mp3player()
{
System.out.print("mp ");
}
}
class MiniPlayer extends Mp3player {
MiniPlayer()
{
System.out.print("mini ");
}
public static void main(String[] args) {
MiniPlayer m = new MiniPlayer();
try {
FileOutputStream fos = new FileOutputStream("dev.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(m); os.close();
FileInputStream fis = new FileInputStream("dev.txt");
ObjectInputStream is = new ObjectInputStream(fis);
MiniPlayer m2 = (MiniPlayer) is.readObject();
is.close();
System.out.println();
} catch (Exception x) {
System.out.print("x ");
}
}
}
采纳答案by Prabhaker A
No.During the process of serialization only the fields of Serializable objects are written out and restored.
不会。在序列化过程中,只有Serializable 对象的字段被写出和恢复。
According to javadocs
根据javadocs
During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class.
Where as the fields of serializable subclasses will be restored from the stream.
在反序列化期间,不可序列化类的字段将使用类的公共或受保护的无参数构造函数进行初始化。
其中可序列化子类的字段将从流中恢复。
Please look into this example
Here ElectronicDevice
is not Serializable
,where as Mp3player
is Serializable
.Observe the fields of respected classes behaviour in serialization process.
请查看此示例
这里ElectronicDevice
不是Serializable
,Mp3player
而是。Serializable
观察序列化过程中受尊重类行为的字段。
import java.io.*;
class ElectronicDevice {
public int i = 0;
protected ElectronicDevice()
{
System.out.println("ed ");
}
}
class Mp3player extends ElectronicDevice implements Serializable {
int j =0;
Mp3player()
{
System.out.println("mp ");
}
}
class MiniPlayer extends Mp3player {
MiniPlayer()
{
System.out.println("mini ");
}
public static void main(String[] args) {
MiniPlayer m = new MiniPlayer();
m.i = 30;
m.j = 40;
try {
System.out.println("i value before serialization: "+m.i);//prints 30
System.out.println("i value before serialization: "+m.j);//prints 40
FileOutputStream fos = new FileOutputStream("dev.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(m); os.close();
FileInputStream fis = new FileInputStream("dev.txt");
ObjectInputStream is = new ObjectInputStream(fis);
MiniPlayer m2 = (MiniPlayer) is.readObject();
is.close();
System.out.println("i value after serialization: "+m2.i);//prints o
System.out.println("j value after serialization: "+m2.j);//prints 40
System.out.println();
} catch (Exception x) {
x.printStackTrace();
System.out.print("x ");
}
}
}
回答by Dev Blanked
Since super class doesn't implement Serializable contents of the super class wont get serialized. Only the contents of the subclass would get serialized. When you deserialize the default constructor of the superclass would get executed and the fields of the superclass initialized as if you invoked the default constructor.
由于超类不实现超类的 Serializable 内容不会被序列化。只有子类的内容会被序列化。当您反序列化超类的默认构造函数时,将执行超类的字段,就像调用默认构造函数一样初始化超类的字段。
Following example illustrates this.
下面的例子说明了这一点。
public class SerializationTest {
public static class Base {
private String name;
public Base() {
this.name = "johnDow";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Sub extends Base implements Serializable {
private static final long serialVersionUID = 1L;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public static void main(String[] args) throws Exception {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteArrayOS);
Sub s = new Sub();
s.setName("name");
s.setAge("10");
out.writeObject(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArrayOS.toByteArray()));
Sub d = (Sub) ois.readObject();
System.out.println(d.getName() + "-" + d.getAge());
}
}
What gets printed is
打印出来的是
johnDow-10
回答by Terry Li
my understanding is super class is also gets serialized due to extends.let me know my understanding is correct or not.
我的理解是超类也由于扩展而被序列化。让我知道我的理解是否正确。
The short answer is NO.
简短的回答是否定的。
In java, every class is a subclass of Object
. Does Object
itself implement Serializable
?
在 Java 中,每个类都是Object
. 是否Object
本身实现Serializable
?
回答by Gabriel Aramburu
This is the rule for superclass serialization:
这是超类序列化的规则:
If you are a serializable class, but your superclass is NOT serializable, then any instance variables you INHERIT from that superclass will be reset to the values they were given during the original construction of the object. This is because the nonserializable class constructor WILL run.
如果您是一个可序列化的类,但您的超类不可序列化,那么您从该超类继承的任何实例变量都将重置为它们在对象的原始构造期间给定的值。这是因为不可序列化的类构造函数将运行。
Therefore, if you add some instance variables to ElectronicDevice, be aware that the superclass 's state will be not serialized. (unless the superclass implements Serializable)
因此,如果向 ElectronicDevice 添加一些实例变量,请注意超类的状态不会被序列化。(除非超类实现了 Serializable)
回答by keerthy
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”
为了允许不可序列化类的子类型被序列化,子类型可能负责保存和恢复超类型的公共、受保护和(如果可访问)包字段的状态”
Reference - http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
参考 - http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html