繁体   English   中英

继承具有指定Type的泛型类时,是否会发生Type Erasure?

[英]Does Type Erasure occur when inheriting a generic class with a specified Type?

当我有一个List<Foo>字段时,由于类型擦除,它在编译时将只是List

但是如果我使用

class FooList extends List<Foo>


我的FooList是编译时的Foo列表还是Object列表?

重要的是要注意以下几点:

  1. 类不能从List extends ,因为它是一个接口,在这种情况下,您应该implements
  2. 构建项目后,您的字节码(.class文件)应如下所示: public abstract class FooList implements List<Foo> ,因此它是Foo的简单列表。
  3. 请注意这种方法,请使用FooList并使用简单的List来查找以下代码:

    公共班级主要{

     public static void main(String[] args) { //works List<Foo> foos = new ArrayList<>(); //don't work FooList fooList = new ArrayList<>(); FooList fooList2 = new ArrayList(); //work, but not necessary FooList fooList3 = new FooList() { //implement all method from List interface }; } 

    }

暂无
暂无

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

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