当我有一个字段 List<Foo>
, 它将变成 List
由于类型删除,在编译时。
但是如果我使用会发生什么
class FooList extends List<Foo>
?
是我的FooList
Foo
列表或 Object
的列表在编译时?
最佳答案
注意几点很重要:
- 一个类不能
extends
来自 List 因为它是一个接口(interface),你应该implements
在这种情况下。 - 构建项目后,您的字节码(.class 文件)应如下所示:
public abstract class FooList implements List<Foo>
,所以它是一个简单的 Foo 列表。 请注意这种方法,使用您的 FooList 并使用一个简单的 List 来查看这段代码:
公共(public)课主要{
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 }; }
关于java - 继承指定Type的泛型类时会发生Type Erasure吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345065/