简体   繁体   English

如何声明扩展参数化类型的类型变量?

[英]How do I declare a type variable that extends a Parameterised type?

I'm not much of a Java programmer so excuse me if my terminology is a bit off. 我不是Java程序员,所以如果我的术语有点偏离,请原谅。 I'd like to declare the following: 我想声明以下内容:

public class Foo<T extends Bar<I, J, K>> {
    ....
};

My IDE tells me that it can't resolve I, J or K. 我的IDE告诉我它无法解析I,J或K。

Is what I'm trying to do possibly in Java or should I take some other approach? 我正在尝试用Java做的事情还是应该采用其他方法?

You have to declare all the type parameters : 您必须声明所有类型参数:

public class Foo<I,J,K,T extends Bar<I, J, K>> {
    ....
}

You can't declare them implicitly this way, you need to write 您不能以这种方式隐式声明它们,您需要编写

public class Foo<I, J, K, T extends Bar<I, J, K>> 

Note if this seems redundant, you might find all you need is 请注意,如果这似乎多余,您可能会发现所需的只是

public class Foo<I, J, K> {
     final Bar<I, J, K> bar;

Assuming that I,J,K are not classes and they are type-parameter names, the compiler needs to refer into an explicit declaration of them. 假设I,J,K不是类,并且它们是类型参数名称,则编译器需要引用它们的显式声明。

This would work 这会工作

class Foo<I,J,K,T extends Bar<I, J, K>> {

}

Given that Bar expects 3 type-params, 鉴于Bar需要3种类型参数,

class Bar<I,J,K>{

} 

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

相关问题 如何在 Java 中声明某种类型的变量? - How do I declare a variable of certain type in Java? 如何声明变量类型的对象 - How to declare an object of a variable type C班 <V extends A> 和(随着anyMethod在类A中返回A),如何将anyMethod返回的对象分配给V型引用变量? - In class C<V extends A> and (with anyMethod returning A in class A), how do I assign an object returned by anyMethod to reference variable of type V? 声明扩展2个不同接口的类型属性 - Declare attribute of type that extends 2 different interfaces 声明流口水中的新类型,以泛型扩展类 - Declare new type in drools that extends a class with generic 如何在Java中动态声明实例的泛型类型 - How do I declare the generic type of an instance dynamically in java 我如何声明一个实现了可限制类类型的接口的类? - How do I declare a class that implements an interface that constrains the type of a class? 如何覆盖Iterable <interface> 将类型方法转换为Iterable <? extends interface> 返回类型方法 - How do I Override a Iterable<interface> return type method into Iterable<? extends interface> return type method 如何在 Frege 中声明带有类型变量的本机接口? - How do I declare a native interface with type variables in Frege? 获取参数化类型的.class? - Getting the .class of a parameterised type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM