java.io.Serializable的使用

 

--对于写出对象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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值