由于我们可以通过覆盖writeObject()和readObject()来覆盖默认的序列化过程,那么Externalizable接口的需求是什么?
答案 0 :(得分:3)
实施Serializable
的类可能或可能希望更改写入流的该类实例的格式。
但是,实施Externalizable
的班级必须实施writeExternal
和readExternal
方法,并将班级的责任推广到写入并恢复数据到流/从流。
答案 1 :(得分:2)
this question has some reasonable answer here
"The CPU cost of serializing Java objects can be significant. This expense, in turn, affects JMS Object messages. You can offset this cost, to some extent, by having application objects implement java.io.Externalizable, but there still will be significant overhead in marshalling the class descriptor. To avoid the cost of having to write the class descriptors of additional objects embedded in an Object message, have these objects implement Externalizable, and call readExternal and writeExternal on them directly. For example, call obj.writeExternal(stream) rather than stream.writeObject(obj). Using Bytes and Stream messages is generally a preferred practice."
this is The best rationale I've been able to find (in Oracle documentation) is in the WebLogic JMS Best Practice document:
答案 2 :(得分:-1)
Serializable接口是用于获取自动序列化功能的工具,但如果您想提供自己的序列化逻辑(自定义逻辑),则可以使用Externalizable接口。 Externalizable接口包含两个必须实现的方法,即 readExternal()和 writeExternal()。
如果您实现了Serializable接口,则默认(自动)序列化过程会处理包括所有基类(超类)状态在内的所有内容。