--对于写出对象employee (employee调用父类方法setName()):
try (ObjectOutputStream oos = new ObjectOutputStream(
Files.newOutputStream(Paths.get("io/employee-infos.bin")))) {
Employee employee = new Employee();
employee.setEmployeeId(123L);
employee.setName("Jim");
oos.writeObject(employee);
}
System.out.println("---Writing done");
try(ObjectInputStream ois = new ObjectInputStream(
Files.newInputStream(Paths.get("io/employee-infos.bin")))) {
Employee employee = (Employee) ois.readObject();
System.out.printf("employee.id: %s, employee.name: %s\n",
employee.getEmployeeId(),
employee.getName());
}
-- 父类Person未实现Serializable, 子类实现Serializable, 父类Person的字段name不会被序列化
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Employee extends Person implements Serializable {
private static final long serialVersionUID = 2187439441699167117L;
private Long employeeId;
public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
}
-- 父类Person实现Serializable,父类Person的字段name,子类的字段都可以正常序列化
class Person implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Employee extends Person {
private static final long serialVersionUID = 2187439441699167117L;
private Long employeeId;
public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
}
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. (序列化类的所有子类都是可序列化的)
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. (为了确保在不同的java编译实现下serialVersionUID的一致性,一个可序列化类必须声明一个显式的serialVersionUID值。同时也强烈建议将serialVersionUID设置为private,这样的话该serialVersionUID就只被声明它的类使用,而不会被继承类访问到)
Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.
Since:
JDK1.1
See Also:
ObjectOutputStream, ObjectInputStream, ObjectOutput, ObjectInput, Externalizable
Author:
unascribed
我们可以让父类实现java.io.Serializable,然后在子类中声明serialVersionUID。