我无法消除我的困惑,
使用外部化是因为我们可以使用 read/writeExternal 方法给出我们自己的(更有效的)序列化-反序列化实现。并控制序列化过程。
我们还可以使用 read/writeObject 方法,序列化过程将被覆盖。
那么为什么还需要Externalized?
为什么有人更喜欢读/写外部而不是读/写对象?
最佳答案
只是序列化情况下的定制。
原因在 Externalizable 中给出文档
The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods. Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream. **
关于java - 当读/写对象方法存在于可序列化中时,为什么要外部化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532046/