简体   繁体   中英

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. 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.

Is what I'm trying to do possibly in Java or should I take some other approach?

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.

This would work

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

}

Given that Bar expects 3 type-params,

class Bar<I,J,K>{

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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