[英]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.