0

我们可以使用 readResolve 和 writeReplace 方法为 Externalizable 和 Serializable 实例指定替换对象......

只是想知道几件事:

1) readObject(ObjectInputStream is ) 和 writeObject(ObjectOutputStream os) 方法能否与可序列化类的 readResolve() 和 writeReplace() 结合使用?是否可以替换对象,然后使用 readObject 和 writeObject 对字节流执行修改操作 2)Exteralizable 的 writeExternal 应显式与超类型协调以保存其状态。只是想知道 Serializable 超类也是如此(但是not Externalizable) 或 Serilizable 类状态在我们将实例写入流时自动保存?

4

1 回答 1

0

如果 Serializable 类存在多个特殊方法,则方法的执行顺序

这些方法按以下顺序执行:-

发生序列化时,方法的执行顺序如下:-

writeReplace
writeObject
readObject
readResolve
validateObject

回答第 1 点)

因此,问题 1 的答案是肯定的

回答第 2 点)

来自 JavaDoc Serializable 接口 Javadoc,用于特殊方法,例如

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException

The writeObject method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.
于 2014-07-28T18:03:48.773 回答