2014-01-26 44 views
0

我們可以採取使用的readResolve和writeReplace方法指定兩個外部化和序列化的情況下,更換對象...關於外部化和序列化問題

只是想知道兩件事情:

1)能的readObject (ObjectInputStream是)和writeObject(ObjectOutputStream os)方法與用於Serilaizable類的readResolve()和writeReplace()一起使用? 是否有可能替換對象,然後使用readObject和writeObject對字節流執行修改操作2)可外部化的writeExternal應該顯式地協調超類型以保存其狀態。只是想知道對於Serializable超類還是如此(但不是Externalizable),或者當我們將實例寫入流時,Serilizable類的狀態會自動保存?

回答

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.