[英]Does Type Erasure occur when inheriting a generic class with a specified Type?
当我有一个List<Foo>
字段时,由于类型擦除,它在编译时将只是List
。
但是如果我使用
class FooList extends List<Foo>
?
我的FooList
是编译时的Foo
列表还是Object
列表?
重要的是要注意以下几点:
extends
,因为它是一个接口,在这种情况下,您应该implements
。 public abstract class FooList implements List<Foo>
,因此它是Foo的简单列表。 请注意这种方法,请使用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.