简体   繁体   English

Java Generics:对一个通用超类进行子类化 - 是一个可再生类型的子类?

[英]Java Generics: Subclassing a generic superclass - is subclass a reifiable type?

I'm reading the SCJP by mughal (3rd edition) (the best SCJP book i have come across) and on page 727, it says the following: 我正在阅读SCJP by mughal(第3版)(我遇到的最好的SCJP书)和第727页,它说如下:

class MyIntList extends ArrayList <Integer> {} // A reifiable subclass  

Now i'm just a bit puzzled here. 现在我在这里有点困惑。 I do know the subclass is of a non-generic type but since it extends ArrayList <Integer> and the parameterized <Integer> will be erased eventually, and this subclass will inherit properties of it's superclass, why would this subclass MyIntList be a reifiable type? 我知道子类是非泛型类型但是因为它扩展了ArrayList <Integer>并且参数化的<Integer>最终将被擦除,并且这个子类将继承它的超类的属性,为什么这个子类MyIntList是一个可再生的类型?

Because every instance of MyIntList is a Array<Integer> , you can find out the lower/upper bounds using reflection. 因为MyIntList每个实例都是一个Array<Integer> ,所以您可以使用反射找出下限/上限。

If you said class MyNumberList<T extends Number> extends ArrayList<T> { ... } , you can still find the bounds, but not exactly what T was for a particular instance of MyNumberList . 如果你说class MyNumberList<T extends Number> extends ArrayList<T> { ... } ,你仍然可以找到边界,但不确定T对于MyNumberList的特定实例是MyNumberList

from The Java Language Specification http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html 来自Java语言规范http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html

4.7 Reifiable Types 4.7可恢复类型

Because some type information is erased during compilation, not all types are available at run time. 由于在编译期间会删除某些类型信息,因此并非所有类型都在运行时可用。 Types that are completely available at run time are known as reifiable types. 在运行时完全可用的类型称为可重新类型。 A type is reifiable if and only if one of the following holds: 当且仅当下列之一成立时,类型才可以恢复:

  • It refers to a non-generic type declaration. 它指的是非泛型类型声明。
  • It is a parameterized type in which all type arguments are unbounded wildcards (§4.5.1). 它是一种参数化类型,其中所有类型参数都是无界通配符(第4.5.1节)。
  • It is a raw type (§4.8). 它是原始类型(§4.8)。
  • It is a primitive type (§4.2). 它是一种原始类型(§4.2)。
  • It is an array type (§10.1) whose component type is reifiable. 它是一种数组类型(第10.1节),其组件类型是可恢复的。

The generic type is erased during compilation of ArrayList, but not during compilation of MyIntList. 泛型类型在编译ArrayList期间被擦除,但在编译MyIntList期间不会被擦除。 So during runtime you can find out that MyIntList extends ArrayList by calling MyIntList.class.getGenericSuperclass() and analyzing the returned object. 因此,在运行时,您可以通过调用MyIntList.class.getGenericSuperclass()并分析返回的对象,找出MyIntList扩展ArrayList。

The reason why this is possible is that MyIntList itself is not generic, but extends a concrete instantiation of ArrayList. 这是可能的原因是MyIntList本身不是通用的,但扩展了ArrayList的具体实例。 So this information can just be stored in the Class object of MyIntList (from where it is accessible using the method mentioned above). 因此,此信息可以存储在MyIntList的Class对象中(使用上述方法可以访问它)。

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

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