英文:
Java Partial Serialization
问题
我有几个相互关联的类
public class A implements java.io.Serializable{
public B objB;
}
public class B {//不实现序列化
}
当我尝试序列化 A 时,由于 B 不可序列化,所以会出错。我想要做的是序列化 A,在反序列化后重新加载 B。
英文:
I have a few classes linked to each other
public class A implements java.io.Serializable{
public B objB;
}
public class B {//doesn't implement serializable
}
When I try to serialize A, I get error because B is not serializable
What I want to do is to serialize A and load B again after de-serializing
答案1
得分: 6
使用 transient 关键字在序列化过程中忽略 objB:
> 最简单的技术是将包含敏感数据的字段标记为 private transient。transient 字段不会持久化,也不会被任何持久化机制保存。
public class A implements java.io.Serializable {
public transient B objB;
}
英文:
use transient for ignore objB during serialization:
> The easiest technique is to mark fields that contain sensitive data as
> private transient. Transient fields are not persistent and will not be
> saved by any persistence mechanism.
public class A implements java.io.Serializable {
public transient B objB;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论