序列化包含对象的对象java [英] Serialize objects containing objects java
问题描述
我尝试序列化一个对象,该对象包含另一个本身可序列化的对象.
I try to serialize an object that contains an other object which is itself serializable.
有没有办法让它工作?当我序列化第一个对象时,指向另一个对象的指针似乎丢失了.
Is there a way to make it work ? The pointer to the other object seems to be lost when I serialize the first object.
推荐答案
这里有两个关于持久对象的规则:
Here are two rules concerning persistent objects:
- 规则 1:要持久化的对象必须实现 Serializable 接口或从其对象层次结构继承该实现
- 规则 2:要持久化的对象必须将所有不可序列化的字段标记为瞬态
查看文章 发现 Java 序列化 API 的秘密".它包括源代码和对序列化过程的很好的解释.
Check the article "Discover the secrets of the Java Serialization API". It's include source code and a good explanation of the serialization process.
回答如何序列化对象树?"的源代码也可以在 http://docs 上找到.oracle.com/javase/8/docs/technotes/guides/rmi/faq.html#tree
Source code answering "How do I serialize a tree of objects?" is also available at http://docs.oracle.com/javase/8/docs/technotes/guides/rmi/faq.html#tree
要了解一些幕后"详细信息,请查看这篇优秀文章:Java 序列化算法揭晓.
To know some "under the hood" details check this excellent article: The Java serialization algorithm revealed.
这篇关于序列化包含对象的对象java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!