ObjectOutputStream
的write方法的方法签名是
public final void writeObject(Object obj) throws IOException
因为 obj
应该实现 Serializable
(了解标记)。
为什么java开发者不把这个方法写成
public final void writeObject(Serializable obj) throws IOException
有什么理由吗?
最佳答案
writeObject 定义在 ObjectOutput 接口(interface)中,其 API 表示实现此接口(interface)的类定义了对象的写入方式
。这意味着理论上可能存在 ObjectOutputStream 以外的实现,它们可能使用其他序列化方式,不需要对象是可序列化的。
关于Java序列化,writeObject(Object obj) 为什么不用writeObject(Serializable obj),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144663/