简体   繁体   English

序列化一个类对象,并将其反序列化为另一类

[英]Serialization one class object, and deserialize it as another class

Do everybody know if in Java exist possibility to serialize MyClass object and deserialize it as MyAnotherClass object? 大家是否知道在Java中是否有可能序列化MyClass对象并将其反序列化为MyAnotherClass对象?

MyClass object has fields as below: MyClass对象具有以下字段:

public class MyClass implements Serializable {

 private String text;
 private int number;
 private String abbrv;

}

And MyAnotherClass : 和MyAnotherClass:

public class MyClass implements Serializable {

 private String text;
 private int number;
 private String abbrv;
 private boolean b; 
 private List<String> myList;

}

If it's possible, please tell me how to do that. 如果可以的话,请告诉我该怎么做。

You can if the originally serialized object's class contains a 如果原始序列化对象的类包含一个

private Object readResolve()

method, or if it contained a 方法,或者如果包含

private Object writeReplace()

method prior to serialization, or if you use a trusted subclass of ObjectInputStream that suitably overrides resolveObject(), or a trusted subclass of ObjectOutputStream that overrides replaceObject(). 序列化之前的方法,或者如果您使用适当覆盖resolveObject()的ObjectInputStream的受信任子类,或者使用覆盖replaceObject()的ObjectOutputStream的受信任子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM