当一个内部类型为原始类型时,为什么嵌套的泛型类型被擦除到最外层?

时间:2019-03-26 12:23:41

标签: java

以下代码段应该能够给我留下我想要表达的印象:

interface TypedInderface <Type> {

    interface AnyTypedThing<T> extends Iterable<T> {}

    public AnyTypedThing<Type> a();
    public AnyTypedThing<AnyTypedThing<Type>> b();
    public AnyTypedThing<AnyTypedThing<AnyTypedThing<Type>>> evenFurtherBeyond();

    public static void main(String[]a){

        TypedInderface raw = new TypedInderfaceImplementation();

        AnyTypedThing single = raw.a();
//seems legit AnyTypedThing<Type> got erased to AnyTypedThing because Type is raw

        AnyTypedThing<AnyTypedThing> nested = raw.b();
//nope AnyTypedThing<AnyTypedThing<Type>> got erased to AnyTypedThing because Type is raw?


//consequently:

        for(Object a : raw.a());
//compiles

        for(AnyTypedThing b : raw.b());
//nah, these are {@code Object}s in that iterable AnyTypedThing

        for(AnyTypedThing<AnyTypedThing> c : raw.evenFurtherBeyond());
//nay, these are{@code Object}s in that iterable AnyTypedThing

    }

}

为什么嵌套类型参数的原始类型会将类型擦除到最外面的类型类而不是原始类型类?

0 个答案:

没有答案