简体   繁体   English

嵌套泛型类型参数参数

[英]Nested Generic Type Parameters Parameters

I have an abstract class which is defined as: 我有一个抽象类,定义为:

public abstract class BaseClass<T extends FirstClass, U extends BaseAnother<? extends SecondClass>> {

I don't wanna use question mark ( ? ) and use some generic variable as like T or U . 我不想使用问号( ? )并使用一些通用变量,例如TU

How can I do that? 我怎样才能做到这一点?

It is public abstract class BaseClass<X extends SecondClass, T extends FirstClass, U extends BaseAnother<X>> { , no? 它是public abstract class BaseClass<X extends SecondClass, T extends FirstClass, U extends BaseAnother<X>> { ,不?

(I've added X extends SecondClass ) (我添加了X扩展SecondClass

Think about how you will use this type variable. 考虑一下如何使用此类型变量。 The way declare it when defining class is to path it in new BaseClass< /* type parameters - here */> { ... } . 定义类时声明它的方法是在new BaseClass< /* type parameters - here */> { ... }中将其路径new BaseClass< /* type parameters - here */> { ... }

Also note, according to JLS , type parameters are declared in class definition in one place: 还要注意,根据JLS ,类型参数在类定义中的一个地方声明:

NormalClassDeclaration: ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt ClassBody NormalClassDeclaration:ClassModifiersopt类标识符TypeParametersopt Superopt接口opt ClassBody

See example there below from 8.1.2. 请参阅下面的8.1.2中的示例 Generic Classes and Type Parameters chapter: 通用类和类型参数一章:

interface ConvertibleTo<T> {
    T convert();
}
class ReprChange<T extends ConvertibleTo<S>,
                 S extends ConvertibleTo<T>> { 
    T t; 
    void set(S s) { t = s.convert();    } 
    S get()       { return t.convert(); } 
}

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

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