简体   繁体   English

如何序列化类型X的列表,填充Y类型的对象,它扩展X,并在反序列化时仍然获得Y元素?

[英]How to serialize a list of type X, filled with objects of type Y, which extends X, and still get Y elements upon deserialization?

I have this list 我有这个清单

LinkedList<ASD> list = new LinkedList<ASD>();

then, I add some objects which extend ASD 然后,我添加一些扩展ASD的对象

BSD bsd = new BSD(); // BSD extends ASD
list.add(bsd);

and serialize list. 并序列化列表。

How can I get it to serialize and deserialize teh bsd element as BSD and not as ASD? 我怎样才能将它作为BSD序列化和反序列化为bhd元素而不是ASD?

It is not a problem. 这不成问题。 All objects that you want to serialize must be instances of classes that implement Serializable . 要序列化的所有对象必须是实现Serializable的类的实例。 LinkedList does it. LinkedList做到了。 Declare either ASD or BSD (it does not matter in your case) to implement Serializable and try. 声明ASDBSD (在您的情况下无关紧要)来实现Serializable并尝试。 Everything should work. 一切都应该有效。

As noted by AlexR, everything is fine: 正如AlexR所说,一切都很好:

class ASD implements Serializable {
  private static final long serialVersionUID = 1L;
}

class BSD extends ASD {
  private static final long serialVersionUID = 1L;
}

LinkedList<ASD> list = new LinkedList<ASD>();
list.add(new BSD())

Now, if you serialize list , and later deserialize it, its elements will be of the correct type BSD . 现在,如果你序列化list ,然后反序列化它,它的元素将是正确的BSD类型。

Furthermore, the generic type argument (the <ASD> thing) is not saved during the serialization. 此外,在序列化期间不保存泛型类型参数( <ASD>事物)。 The serialization procedure will only remember that list is a LinkedList object. 序列化过程只会记住listLinkedList对象。 What this means is that you can serialize an object of type LinkedList<ASD> and then deserialize it as LinkedList<BSD> (or even as eg LinkedList<Integer> ). 这意味着您可以序列化LinkedList<ASD>类型的对象,然后将其反序列化为LinkedList<BSD> (或甚至例如LinkedList<Integer> )。 An exception can be thrown later, though, if you try to access an element of the list that cannot be cast to the newly specified generic type. 但是,如果您尝试访问无法转换为新指定的泛型类型的列表元素,则可以稍后抛出异常。

Finally, I'm just saying this is doable, not that it is a good practice :) 最后,我只是说这是可行的,而不是这是一个很好的做法:)

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

相关问题 杰克逊(Jackson):B类扩展了A。X类扩展了Y。A有一个X列表。我如何反序列化Y并在其中获得Y列表? - Jackson: Class B extends A. Class X extends Y. A has a List of X. How can I deserialize Y and get a List of Ys in it? 方法 x 对于类型 y 是未知的 - The method x is unidentified for type y @SuppressWarnings ArrayList <X> 可能不包含Y类型的对象 - @SuppressWarnings ArrayList<X> may not contain objects of type Y 如何获得List <Y> 来自地图 <X,Y> 通过提供List <X> 使用谷歌收藏? - How to get List<Y> from Map<X,Y> by providing List<X> using google collection? Java JUnit:方法X对于类型Y是不明确的 - Java JUnit: The method X is ambiguous for type Y 如何在javacv中获取提取对象的x,y坐标? - How to get x,y coordinates of extracted objects in javacv? 获取在笛卡尔坐标上表示x,y的值的列表 - get a list of values to represent x,y on Cartesian 如何使用“x <= y && x> = y && x!= y”使循环无限? - How to make loop infinite with “x <= y && x >= y && x != y”? 如果对象(x,y)靠近其他对象(x,y) - If objects (x,y) are near other objects (x,y) 如何在Java中获取程序窗口的x和y? - How to get the x and y of a program window in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM