Domanda

I have some problem in defining a Java5 method by using Generics.

I experimented that I can define a method as follows:

public <R extends Runnable & MyInterface, T> void submit(R runnable, T task);

So, submit only accept an instance of an object of class R that implements the interfaces Runnable and MyInterface. Now, suppose I want to do something like follows:

public <C extends Callable & MyInterface, T> submit(C<T> callable);

where C is a Callable that also implements MyInterface and has a return value of type T. In this case, the IDE reports an error in the firm of the method:

required: class
found: type parameter C
where C,T are type-variables

What is the right way to define the second submit method?

PS: I don't know if the title is the best one. Please, feel free to improve the title once you identify the problem type!

È stato utile?

Soluzione

Type parameters can't be given other type parameters, e.g. C<T> - but they can be bounded by parameterized types. Use the following:

public <C extends Callable<T> & MyInterface, T> submit(C callable);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top