简体   繁体   English

Java - 类<? extends Collection>

[英]Java - Class<? extends Collection>

I would like to add multiple class in below jackson deserialize.我想在下面的jackson反序列化中添加多个类。 But how do i do that?但是我该怎么做呢? below is the sample code.下面是示例代码。

XmlMapper mapper = new XmlMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector);
TypeFactory typeFactory = mapper.getTypeFactory();
Class<? extends Collection> list;
MapType mapType = typeFactory.constructRawCollectionType(list);
File is = new File("myXMLFile.xml");
ArtworkContentMessageType je = mapper.readValue(is,mapType);

below part is confusing me.下面的部分让我很困惑。 How to add class to list?如何将班级添加到列表中?

Class<? extends Collection> list;
MapType mapType = typeFactory.constructRawCollectionType(list);

how do i add multple class(ex: Body.class and P.class).我如何添加多个类(例如:Body.class 和 P.class)。 I try below and its not the right object because its not a Class type.我在下面尝试,它不是正确的对象,因为它不是 Class 类型。

List<Class> clazz = new ArrayList<>();
clazz.add(ArtworkContentMessageType.class);
clazz.add(Body.class);
clazz.add(P.class);
MapType mapType = typeFactory.constructRawCollectionType(clazz);

java.lang.Class is a generic class, meaning it can take parameters, as per this syntax: java.lang.Class是一个泛型类,这意味着它可以按照以下语法接受参数:

Class<T>

where T is the type of the class modeled by the Class object.其中 T 是由 Class 对象建模的类的类型。 Taking the same example as in the documentation, the type of String.class is Class<String> .以文档中的示例为例, String.class的类型是Class<String>

As such, by using the wildcard syntax , you can declare an instance of a Class without knowing its type: Class<?> .因此,通过使用通配符语法,您可以在不知道其类型的情况下声明Class的实例: Class<?>

If you don't know the type but you know it will extend a given class, you can use this syntax: Class<? extends Superclass>如果您不知道类型但知道它会扩展给定的类,则可以使用以下语法: Class<? extends Superclass> Class<? extends Superclass> . Class<? extends Superclass>

So this syntax:所以这个语法:

Class<? extends Collection> list;

declares a class whose type is a subclass of Collection .声明一个类,其类型是Collection的子类。

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

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