Java 的序列化 (Serialization) 教程
序列化中的继承:
现在我们看看继承是如何影响序列化的。不管父类是不是可序列化,这将引出很多个例子。如果父类是非可序列化的,我们将如何处理,并且它是如何工作的。让我们看看例子。
我们将创建一个Person.java,作为 Employee的父类。
案例4: 如果父类是可序列化的
如果父类可序列化,那么所有的继承类将是可序列化的。
案例5: 如果父类为非可序列化呢?
如果父类为非可序列化的 ,那么我们的处理办法会很不一样。
如果父类为非可序列化的,那么它必然不会有参数构造函数。
Person.java
package org.arpit.javapostsforlearning;public class Person { String name=”default”; String nationality; public Person() { System.out.println(“Person:Constructor”); } public Person(String name, String nationality) { super(); this.name = name; this.nationality = nationality; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNationality() { return nationality; } public void setNationality(String nationality) { this.nationality = nationality; }} 在包org.arpit.javapostsforlearning 中创建Employee.java Employee.java: package org.arpit.javapostsforlearning;import java.io.Serializable;public class Employee extends Person implements Serializable{ int employeeId; String department; public Employee(int employeeId,String name,String department,String nationality) { super(name,nationality); this.employeeId=employeeId; this.department=department; System.out.println(“Employee:Constructor”); } public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; }} 在org.arpit.javapostsforlearning包中创建SerializeDeserializeMain.java SerializeDeserializeMain.java: package org.arpit.javapostsforlearning;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializeDeserializeMain { /** * @author Arpit Mandliya */ public static void main(String[] args) { //Serialize Employee emp = new Employee(101,”Arpit”,”CS”,”Indian”); System.out.println(“Before serializing”); System.out.println(“Emp id: ” + emp.getEmployeeId()); System.out.println(“Name: ” + emp.getName()); System.out.println(“Department: ” + emp.getDepartment()); System.out.println(“Nationality: ” + emp.getNationality()); System.out.println(“************”); System.out.println(“Serializing”); try { FileOutputStream fileOut = new FileOutputStream(“employee.ser”); ObjectOutputStream outStream = new ObjectOutputStream(fileOut); outStream.writeObject(emp); outStream.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } //Deserialize System.out.println(“************”); System.out.println(“Deserializing”); emp = null; try { FileInputStream fileIn =new FileInputStream(“employee.ser”); ObjectInputStream in = new ObjectInputStream(fileIn); emp = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println(“Employee class not found”); c.printStackTrace(); return; } System.out.println(“After serializing”); System.out.println(“Emp id: ” + emp.getEmployeeId()); System.out.println(“Name: ” + emp.getName()); System.out.println(“Department: ” + emp.getDepartment()); System.out.println(“Nationality: ” + emp.getNationality()); }} 运行:
当你运行SerializeDeserializeMain.java后,你会得到如下的输出,如果父类是非可序列化的,那么在反序列化过程中,所有继承于父类的实例变量值,将会通过调用非序列化构造函数来初始化。 这里 name继承于person,所以在反序列化过程中,name将会被初始化为默认值。