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..."