为什么 ObjectOutputStream.writeObject 不采用可序列化?

发布于 2024-10-31 10:59:47 字数 354 浏览 0 评论 0 原文

为什么 < code>ObjectOutputStream.writeObject(Object o) 不采取 可序列化?为什么它需要一个对象

Why does ObjectOutputStream.writeObject(Object o) not take a Serializable? Why is it taking an Object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-11-07 10:59:47

这是因为 ObjectOutputStream 中的 writeObject 覆盖了 ObjectOutput 接口,它不要求对象可序列化

ObjectOutput 接口指定允许将对象写入流或底层存储的方法,但这可以通过序列化以外的过程来实现。 ObjectOutputStream 实现此功能,但需要可序列化的对象。但是,它无法修改其实现的接口的签名。

This is because writeObject in ObjectOutputStream overrides the method in the ObjectOutput interface which does not require that the object be Serializable.

The ObjectOutput interface specifies methods that allow objects to be written to a stream or underlying storage, but this may be achieved by a process other than serialization. The ObjectOutputStream implements this functionality, but requires serializable objects. However, it cannot modify the signature of the interface that it implements.

浴红衣 2024-11-07 10:59:47

它应该是ObjectOutputStream.writeObject(serialized)而不是ObjectOutputStream。 writeObject(对象)。这是一个正确的用例,应该使用像 Serialized 这样的标记接口,但不幸的是没有。这将使编译时类型检查的真正好处成为可能,而不是在对象未实现 Serialized 接口时在运行时失败。

我想借此机会提一下 Joshua Bloch 在他的书中提到的内容 有效的java

标记接口是不包含任何方法的接口
声明,但仅仅指定(或“标记”)一个类
将接口实现为具有某些属性。例如,
考虑可序列化接口。通过实现这个接口,
类表示它的实例可以写入
ObjectOutputStream(或“序列化”)。

对于 Serialized 标记接口,如果其参数不存在,则 ObjectOutputStream.write(Object) 方法将会失败
实现接口。令人费解的是,该书的作者
ObjectOutputStream API 未利用 Serialized
声明 write 方法的接口。方法的参数类型
应该是Serialized而不是Object。就目前情况而言,
尝试在不支持的对象上调用 ObjectOutputStream.write
实现 Serialized 只会在运行时失败,但它没有
就这样。

It should be ObjectOutputStream.writeObject(serializable) rather than ObjectOutputStream. writeObject(Object). It is a proper use case where a marker interface like Serializable should have been used but unfortunately not. This would have made it possible the very real benefit of compile-time type checking instead of failing at runtime if the object does not implement Serializable interface.

I would like to take this opportunity to mention what Joshua Bloch has mentioned in his book Effective java:

A marker interface is an interface that contains no method
declarations, but merely designates (or “marks”) a class that
implements the interface as having some property. For example,
consider the Serializable interface. By implementing this interface, a
class indicates that its instances can be written to an
ObjectOutputStream (or “serialized”).

In the case of the Serializable marker interface, the ObjectOutputStream.write(Object) method will fail if its argument does not
implement the interface. Inexplicably, the authors of the
ObjectOutputStream API did not take advantage of the Serializable
interface in declaring the write method. The method’s argument type
should have been Serializable rather than Object. As it stands, an
attempt to call ObjectOutputStream.write on an object that doesn’t
implement Serializable will fail only at runtime, but it didn’t have
to be that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文