简体   繁体   English

可以在java中扩展泛型类吗?

[英]Is possible extend generic class in java?

I mean我的意思是

public class Generic<T> extends T{
}

if I have two classes, in my case it is Response and Request, and several classes with own fields, i want extend response or request depending on the situation or vice versa extend classes.如果我有两个类,在我的情况下是 Response 和 Request,以及几个具有自己字段的类,我想根据情况扩展响应或请求,反之亦然扩展类。 But i do not want to create another classes但我不想创建另一个类

That's impossible logically per java as by definition of inheritance you always extend a class not a type .这在逻辑上是不可能的,因为根据继承的定义, you always extend a class not a type If you trying to do so, you will see following error:如果您尝试这样做,您将看到以下错误:

Error:(3, 48) java: unexpected type
  required: class
  found:    type parameter T

So, it clear that,所以,很明显,

A class can not inherit from it's type一个类不能从它的类型继承

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

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