在 Java 中,我可以继承由类型参数提供的 class 吗?

In Java, can I inherit a class supplied by a type parameter?

在 java 中,我可以或多或少地做这样的事情吗?以及如何?

public class SomeGenericClass<T> extends T{
}

我认为这是来自 JLS (§8.1.4) 的相关引用:

The ClassType [provided in the extends clause] must name an accessible (§6.6) class type, or a compile-time error occurs.

(关于可访问性的部分无关紧要)。

A class 类型是 not the same thing as a type variable (§4.3)T 是),因此尝试这样做将是一个编译时错误。

public class SomeGenericClass<T> extends T{
}

你必须更好地理解泛型。 SomeGenericClass 是一个模板(C++ 术语)。实际上,它对编译器没有任何意义。你不能写

SomeGenericClass<T> obj = new SomeGenericClass<T>() // this is invalid

相反,您需要提供类型属性。此信息补充了@Andy Turner

提供的上述答案

您不能 public class SomeGenericClass<T> extends T 有多种原因:

类型擦除会将 T 擦除为 Object.,并且说 public class SomeGenericClass<T> extends Object 毫无意义,因为 Object 已经是基础 class所有类型。

想象一下,如果它被允许并且我做了类似 public class SomeGenericClass<T extends Comparable> extends T 的事情,类型擦除会将其变成 public class SomeGenericClass<Comparable> extends Comparable 但接口实现没有扩展。