我们可以使用readResolve和writeReplace方法为Externalizable和Serializable实例指定替换对象......
只是想知道几件事:
1)readObject(ObjectInputStream是)和writeObject(ObjectOutputStream os)方法可以与readResolve()和writeReplace()一起用于Serilaizable类吗? 是否可以替换对象,然后使用readObject和writeObject对字节流执行修改操作 2)Exteralizable的writeExternal应该明确地与超类型协调以保存它的状态。只是想知道Serializable超类也是如此(但不是Externalizable)或者当我们将实例写入流时,Serilizable类状态会自动保存吗?
答案 0 :(得分:0)
Order of execution of methods if multiple special methods are present for a Serializable class
方法按以下顺序执行: -
发生序列化时,方法的执行顺序如下: -
writeReplace
writeObject
readObject
readResolve
validateObject
回答第1点)
因此,问题1的答案是肯定的
回答第2点)
来自JavaDoc Serializable interface 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.