简体   繁体   English

java泛型类强制转换

[英]java generic class cast

Hello i have got hash map with value: 你好我有哈希映射值:

    private static final LinkedHashMap<Class<? extends Annotation> , Class<? extends AGenerator<?>>> annotationGeneratorMap = new LinkedHashMap<Class<? extends Annotation>, Class<? extends AGenerator<?>>>();

and my numeric Generator looks like : 我的数字生成器看起来像:

public abstract class NumericGenerator<T extends Number> extends AGenerator<T>{


public NumericGenerator(Field field) {
    super(field);
    // TODO Auto-generated constructor stub
}

public T random() {
    // TODO Auto-generated method stub
    return null;
}

  }

And i have got problem when i need to put this class into hashmap : 当我需要将此类放入hashmap时,我遇到了问题:

annotationGeneratorMap.put(GenerateNumeric.class, NumericGenerator.class);

And in eclipse i have got error the method is not applicable for the argument ??? 在eclipse中我有错误,该方法不适用于参数???

but : 但是:

        annotationGeneratorMap.put(GenerateNumeric.class, (Class<? extends AGenerator<?>>) NumericGenerator.class);

and @SuppressWarnings("unchecked") is good ..:/ 和@SuppressWarnings(“未选中”)很好......:/

Can i do this without casting ?? 我能不这样做吗? (Class<? extends AGenerator<?>>) NumericGenerator.class

use Class<? extends AGenerator> 使用Class<? extends AGenerator> Class<? extends AGenerator> instead Class<? extends AGenerator>

LinkedHashMap<
    Class<? extends Annotation>, 
    Class<? extends AGenerator> > annotationGeneratorMap = new LinkedHashMap<>()

In Java, class can represent a raw type, but cannot represent a generic type. 在Java中,类可以表示原始类型,但不能表示泛型类型。

There is a class for List , but there is no class for List<String> . List有一个类,但List<String>没有类。

So you can declare Class<? extends List> 所以你可以声明Class<? extends List> Class<? extends List> , which is compatible with ArrayList.class etc. because raw type ArrayList is a subtype of List . Class<? extends List> ,它与ArrayList.class等兼容,因为原始类型ArrayListList的子类型。

But Class<? extends List<?>> 但是Class<? extends List<?>> Class<? extends List<?>> doesn't make much sense, because there is no class that is a subtype of List<?> . Class<? extends List<?>>没有多大意义,因为没有类是List<?>的子类型。

And all thanks to erasure. 一切都要归功于擦除。

Compile time check won't allow you to do that unless you cast it of course. 编译时间检查不允许你这样做,除非你当然把它转换。

Information about generic types is lost at runtime, not compile time. 有关泛型类型的信息在运行时丢失,而不是在编译时丢失。

Following compiles without error on version 1.7.0_02 : 在版本1.7.0_02上编译后没有错误:

import java.util.*;
import java.lang.annotation.*;

interface AGenerator<T> {}

interface A extends Annotation {}
class X implements AGenerator<X> {}

class E7 {
    private static final LinkedHashMap<Class<? extends Annotation>, 
        Class<? extends AGenerator<?>>> annotationGeneratorMap 
        = new LinkedHashMap<Class<? extends Annotation>, 
                            Class<? extends AGenerator<?>>>();


    void foo() {
        annotationGeneratorMap.put(A.class, X.class);
    }

}

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

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