简体   繁体   English

有关可外部化和可序列化的问题

[英]Questions regarding Externalizable and Serializable

We can take use of readResolve and writeReplace methods to designate replacement objects for both Externalizable and Serializable instances... 我们可以使用readResolve和writeReplace方法为Externalizable和Serializable实例指定替换对象...

Just wanted to know couple of things: 只想知道几件事:

1)Can readObject(ObjectInputStream is ) and writeObject(ObjectOutputStream os) methods be used in conjunction with readResolve() and writeReplace() for a Serilaizable class? 1)可以将readObject(ObjectInputStream is)和writeObject(ObjectOutputStream os)方法与readResolve()和writeReplace()一起用于可Serilaizable类吗? Is it possible to replace the object and then perform modifications operations to the byte stream using readObject and writeObject 2)Exteralizable's writeExternal should explicitly coordinate with the supertype to save its state.Just wanted to know that is it true for Serializable super classes also(but not Externalizable) or the Serilizable classes state gets saved automatically when we write the instance to a stream? 是否有可能替换对象然后使用readObject和writeObject对字节流执行修改操作2)Exteralizable的writeExternal应该与超类型显式协调以保存其状态。只想知道Serializable超类也是如此(但是而不是Externalizable)或将实例写入流时会自动保存Serilizable类状态?

Order of execution of methods if multiple special methods are present for a Serializable class 如果存在可序列化类的多个特殊方法,则方法的执行顺序

The methods are executed in the following order:- 这些方法按以下顺序执行:

When serialization occurs , the order of execution of methods is as follows :- 发生序列化时,方法的执行顺序如下:

writeReplace
writeObject
readObject
readResolve
validateObject

Answer to Point 1) 对第1点的回答

Therefore,the answer to question 1 is yes 因此,问题1的答案是肯定的

Answer to Point 2) 对第2点的回答

From JavaDoc Serializable interface Javadoc , for special methods such as 从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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM