java 什么是可序列化?这是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3753413/
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
What is Serializable? What does this mean?
提问by IamIronMAN
Possible Duplicates:
Do Hibernate table classes need to be Serializable?
What does Serializable mean?
public class ExampleEntity implements Serializable
{
@Id
private long id;
private int fieldInt;
private long fieldLong;
private String fieldString;
}
I am looking after JPA tutorials. I am able to understand the basic concepts, but in all the tutorials they have added, this serializable. What is the purpose of this? How does that help me? Any suggestions Please?
我在看 JPA 教程。我能够理解基本概念,但在他们添加的所有教程中,这是可序列化的。这样做的目的是什么?这对我有什么帮助?请问有什么建议吗?
回答by pakore
It means that the class can be serialized. This means that it can be converted into an stream of 0's and 1's and be sent to other service, like a webservice or and ORM (hibernate) or whatever. Then this service knows how store the stream.
这意味着该类可以被序列化。这意味着它可以转换为 0 和 1 的流并发送到其他服务,例如 web 服务或 ORM(休眠)或其他任何服务。然后这个服务知道如何存储流。
Also, your program can receive a serialized class and create an instance for it from the stream of 0's and 1's.
此外,您的程序可以接收一个序列化的类并从 0 和 1 的流中为其创建一个实例。
It's the way to "save" instances of classes and "restore" them at any other moment in time.
这是“保存”类实例并在任何其他时间“恢复”它们的方法。
More info at
更多信息在
- http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html
- http://en.wikipedia.org/wiki/Serialization
- http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html
- http://en.wikipedia.org/wiki/Serialization
To be able to make a class serializable, you must implement the interface "Serializable".
为了能够使类可序列化,您必须实现接口“Serializable”。
Why so? Because when the moment to serialize a class arrive, the function
writeObtject
will be called to convert the object into bytes. On the other hand, when you have the bytes and you want to get the instance of the class, the functionreadObject
will be called.Why is not automatic? A variable value it's already represented by bytes.Because the problem comes for example when a class holds instances of other classes. What do you do? You serialize the other classes as well? You just keep the reference address? It's pretty complex and it may be dependant on your type of project and design.
为什么这样?因为当序列化一个类的时刻到来时,
writeObtject
将调用该函数将对象转换为字节。另一方面,当您拥有字节并且想要获取类的实例时,readObject
将调用该函数。为什么不是自动的?一个已经用字节表示的变量值。因为例如当一个类包含其他类的实例时就会出现问题。你做什么工作?你也序列化了其他类?你只保留参考地址?它非常复杂,可能取决于您的项目和设计类型。
回答by fredley
Serializable classes can be serialized - converted into bytes for storing on a hard drive or transmitting over a network.
可序列化的类可以序列化 - 转换为字节以存储在硬盘驱动器上或通过网络传输。