Java中类序列化构造函数的调用问题

来源:互联网 发布:买家申请淘宝介入后果 编辑:程序博客网 时间:2024/05/05 03:41

public class Person implements Serializable{public Person(){System.out.println("Person...");}}public class Student extends Person{public Student(){System.out.println("Student...");}}
1.Java中的父类实现了序列化接口Serializable接口,那么它的子类也都是可以序列化的。

在对象序列化的过程中,会依次调用"父类的构造函数"->"子类的构造函数",如上例所示,如果对

Student对象进行序列化,将依次调用构造函数<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">"Person..."->"Student..."</strong>

public class Person implements Serializable{public Person(){System.out.println("Person...");}}public class Student extends Person{public Student(){System.out.println("Student...");}}public class CollegeStudent extends Student implements Serializable{public CollegeStudent(){System.out.println("CollegeStudent");}}
2.在对序列化的对象进行反序列化的时候,如果某一个类未实现序列化接口Serializable,那么将显式的调用该类的构造函数,如对上图所示的
CollegeStudent对象进行反序列化,那么将依次显式地调用构造函数"Person..."->"Student..."
0 1
原创粉丝点击