java 谁真正实现了可序列化的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/970891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Who actually implements serializable methods?
提问by Macarse
I've been learning how to use Serializable.
我一直在学习如何使用Serializable.
I know if I create a class 'A' with different variables who implements Serializableand I add Serializableto my class, it's also Serializable.
我知道如果我使用不同的变量创建一个类“A”,Serializable并且我添加Serializable到我的类中,它也是Serializable.
But, who is actually implementing those two methods to serialize?
Does Objecttake care of everything or different classes overloads them when necessary?
但是,谁真正实现了这两种序列化方法?是否Object在必要时处理一切或不同的类重载它们?
采纳答案by erickson
The serialization is actually implemented in java.io.ObjectOutputStream(and java.io.ObjectInputStream) and some of its helper classes. In many cases, this built-in support is sufficient, and the developer simply needs to implement the marker interface Serializable. This interface is called a "marker" because it doesn't declare any methods, and thus doesn't require any special API on implementation classes.
序列化实际上是在java.io.ObjectOutputStream(和 java.io.ObjectInputStream)及其一些帮助类中实现的。在很多情况下,这种内置支持就足够了,开发者只需实现标记接口即可Serializable。这个接口被称为“标记”,因为它不声明任何方法,因此在实现类上不需要任何特殊的 API。
A programmer can add or replace default serialization mechanism with their own methods if needed. For example, if some additional initialization is required after deserializing an object, a method can be added with the following signature:
如果需要,程序员可以用他们自己的方法添加或替换默认的序列化机制。例如,如果反序列化对象后需要一些额外的初始化,则可以添加具有以下签名的方法:
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, java.lang.ClassNotFoundException
For total control over serialization and deserialization, implement java.io.Externalizableinstead of Serializable.
要完全控制序列化和反序列化,请实现java.io.Externalizable而不是Serializable.
There are many other extension points in Java serialization, if needed. The serialization specificationis an authoritative and complete source for learning about all of them.
如果需要,Java 序列化中还有许多其他扩展点。该序列化规范是学习所有这些的权威性和完整的源代码。
回答by Jorn
I suppose the methods you are talking about are readObject()and writeObject(). You only need to implement those if you need to do custom serialization, for example when you have fields in your object that aren't serializable. If you only have serializable fields and primitives, you don't have to implement custom serialization methods.
Also, you can skip some fields on serialization by adding the transientkeyword to them.
我想你所说的方法是readObject()和writeObject()。如果您需要进行自定义序列化,您只需要实现这些,例如当您的对象中有不可序列化的字段时。如果您只有可序列化的字段和原语,则不必实现自定义序列化方法。此外,您可以通过向transient它们添加关键字来跳过序列化中的某些字段。
回答by Michael Borgwardt
Look at the API docof Serializable, it explains the mechanism in detail.
看看API文档的Serializable,它详细解释了该机制。
Basically, you don't have to do anything unless you want more control over how your objects are serialized, in which case there are some "magic" methods you can implement and which will be called by the serialization mechanism.
基本上,除非您想要更多地控制对象的序列化方式,否则您不必做任何事情,在这种情况下,您可以实现一些“魔法”方法,这些方法将由序列化机制调用。
If you want full control, you can use Externalizableinstead.
如果您想要完全控制,可以改用Externalizable。
回答by Cuga
For a class to be serializable, each object contained as a member of that class must also be serializable. Java will run down the tree of all objects referenced by yours and serialize them each in turn.
对于可序列化的类,作为该类成员包含的每个对象也必须是可序列化的。Java 将遍历您引用的所有对象的树,并依次序列化它们。
If you want to have greater control over how objects are serialized, you can implement the Externalizable interface:
如果你想更好地控制对象的序列化方式,你可以实现 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.
Externalizable 接口的 writeExternal 和 readExternal 方法由类实现,以使类完全控制对象及其超类型的流的格式和内容。
回答by Robert Munteanu
I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable.
我知道如果我创建一个具有不同变量的类“A”,这些变量实现了 Serializable 并且我将 Serializable 添加到我的类中,它也是可序列化的。
Yes, this moment your class is Serializable.
是的,此时您的类是可序列化的。
回答by Kosi2801
If you implement a class which has to be serializable, you also have to provide a method which does the serialization in the same class.
如果您实现一个必须可序列化的类,您还必须提供一个在同一个类中进行序列化的方法。
You cannot rely on Object to be able to guess what your class needs to be successfully serialized and deserialized. Think of working variables of your class for example which do not need to be serialized, Object would not be able to differentiate those from the important fields.
您不能依靠 Object 来猜测您的类需要成功序列化和反序列化的内容。想想你的类的工作变量,例如不需要序列化的变量,Object 将无法将这些变量与重要的字段区分开来。

