我们可以使用readResolve和writeReplace方法为可扩展和可序列化的实例指定替换对象。
我只想知道几件事:
1)对于一个Serilaizable类,readObject(ObjectInputStream is )和writeObject(ObjectOutputStream os)方法能否与readResolve()和writeReplace()结合使用?可以替换对象,然后使用readObject和writeObject对字节流执行修改操作吗?Exteralizable的writeExternal应该显式地与超级类型协调,以保存它的state.Just,希望知道是否也适用于可序列化的超类(但不是Externalizable),或者当我们将实例写入流时会自动保存Serilizable类状态?
发布于 2014-07-28 18:03:48
如果一个可序列化类存在多个特殊方法,则方法的执行顺序
这些方法按以下顺序执行:
当序列化发生时,方法的执行顺序如下:
writeReplace
writeObject
readObject
readResolve
validateObject对第1点的答复)
因此,问题1的答案是肯定的。
(对第2点的答复)
,用于特殊方法,如
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.https://stackoverflow.com/questions/21365282
复制相似问题