为什么 java.util.List 不实现 Serialized?
为什么java.util.List
没有实现Serializable
,而LinkedList
、Arraylist
等子类却实现了?这似乎不违背继承原则吗?例如,如果我们想通过网络发送一个 Linkedlist,我们必须这样写:
new ObjectOutputStream(some inputStream).writeObject(some LinkedList);
到目前为止一切都很好,但是在读取另一端的对象时我们必须明确地说 LinkedList l = (LinkedList)objectInputStream.readObject( );
而不是 List l = (List)objectInputStream.readObject();
。如果我们要将写入功能从 LinkedList
更改为 ArrayList
,我们还必须更改读取部分。让 List
实现 Serialized
就可以解决这个问题。
Why is it that java.util.List
does not implement Serializable
while subclasses like LinkedList
, Arraylist
do? Does not it seem to be against inheritance principles? For example if we want to send a Linkedlist over a network, we have to write:
new ObjectOutputStream(some inputStream).writeObject(some LinkedList);
So far so good, but while reading the object on the other side we have to explicity say LinkedList l = (LinkedList)objectInputStream.readObject();
instead of List l = (List)objectInputStream.readObject();
. If we were ever to change the writing functionality from LinkedList
to say ArrayList
, we will also have to change the reading part. Having List
implement Serializable
would have solved the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
List
未实现Serializable
,因为它不是列表的关键要求。不保证(或需要)List
的每个可能实现都可以序列化。LinkedList
和ArrayList
选择这样做,但这特定于它们的实现。其他List
实现可能不是Serialized
。List
does not implementSerializable
because is it not a key requirement for a list. There is no guarantee (or need) that every possible implementation of aList
can be serialized.LinkedList
andArrayList
choose to do so, but that is specific to their implementation. OtherList
implementations may not beSerializable
.List 是一个接口,使其扩展 Serialized 意味着 List 的任何实现都应该是可序列化的。
可序列化属性不是 List 抽象的一部分,因此不应是实现所必需的。
List is an interface and making it extend Serializable would mean that any implementation of List should be serializable.
The serializable property is not part of the List abstraction and should therefore not be required for an implementation.
不。LinkedList 始终是一个 List。当你反序列化链表时,由于 LinkedList 是一个 List,所以你可以写
L in fact a LinkedList 这一事实并不重要。你想要一个列表,你得到了一个列表。
No. A LinkedList is always a List. When you deserialize the linked list, since a LinkedList is a List, you may write
The fact that l is in fact a LinkedList is not important. You wanted a List, and you got a List.
因为
List
也可以由用户特定的子类实现,并且实现者可能不一定希望实现Serialized
。可串行性也不属于List
的关键职责,因此没有理由将两者链接在一起。Because
List
is open to be implemented by user specific subclasses as well, and implementors may not necessarily want to implementSerializable
. Serializability does not belong to the key responsibilities of aList
either, so there is no reason to link the two together.考虑假设的 ThreadList 实现 List,其中包含任何给定时间点的活动线程列表。该实现透明地浏览活动线程并允许轻松访问它们 - 为了您的方便。这样的实现应该是可序列化的(忘记
Thread
是不可序列化的)吗?由实现接口的人来决定她的实现是否可以安全地序列化。
List
太通用了,因为基本上说明了 *T` 类型的项目的有序集合。Consider hypothetical
ThreadList implements List<Thread>
, containing the list of active threads at any given point in time. The implementation transparently browses active threads and allows easy access to them - for your convenience. Should such an implementation be serializable (forgetting thatThread
is not serializable)?It is up to the person implementing the interface to decide, whether her implementation is safe to be serialized.
List
is too generic, as basically stating *ordered collection of items of type T`.你的问题似乎是基于误解。要序列化对象,该对象(或其类)必须实现
Serializable
,但不需要使用Serialized
类型(或某些子类型)的表达式来做这个。writeObject
方法的参数类型为Object
而不是Serialized
,并且返回类型为readObject,这是有意为之的()
。但即使这些参数和返回类型是可序列化的,您也不需要知道具体的实现类型:
并且
将像现在一样工作(无需可序列化转换)。
ObjectInputStream 和 ObjectOutputStream 在调用时根本不关心您的类型,它们只是查看手头的对象及其类。
Your question seems to be based on a misunderstanding. To serialize an object, the object (or its class) has to implement
Serializable
, but you don't need to use an expression of typeSerializable
(or some subtype) to do this. It is quite intentional that thewriteObject
method has a parameter type ofObject
and notSerializable
, as well as the return type ofreadObject()
.But even if these parameter and return types were
Serializable
, you would not need to know the specific implementation types:and
would work as well as it works now (without the Serializable cast).
ObjectInputStream and ObjectOutputStream do not care at all about your types when invoking, they simply look at the object at hand and its class.
如果 List 实现/扩展了 Serialized,那么您就隐含了这样的约定:List 的所有实现类/子类也是可序列化的,但这并不总是正确的。例如,查看 ForwardingListMultimap 的 guava-collections 实现。它在功能上不需要是可序列化的,而这只是因为 List 不可序列化而成为可能。
If List implements/extends Serializable then you have implied the contract that all implementation classes/subclasses of List are also Serializable which is not always true. For e.g look at the guava-collections implementation of ForwardingListMultimap. It doesn't need to be Serializable functionally and this was possible only because List is not Serializable.
因为并不是世界上每个
List
实现都必须是Serializable
。你尝试过吗?如果你这样做,我想你会发现它有效。
Because not every
List
implementation in the world must beSerializable
.Have you tried it? If you do, I think you'll find that it works.
List 扩展了 Collection,它不能实现任何东西,因为它是一个接口......
List extends Collection, and it can't implement anything because it's an interface...