質問

I'm trying to set an upper bound to the Result parameter of an AsyncTask like so:

public class MyTask extends AsyncTask<T, Long, V extends Model> 

The compiler is complaining that 'extends is not expected, it's expecting a comma.

I have tried writing Model as an abstract class and as a regular class.

Any ideas?

Thank you, David

役に立ちましたか?

解決

Since T and V are unresolved type parameters, MyTask needs to be parameterized on them. Try declaring the following:

public class MyTask<T, V extends Model> extends AsyncTask<T, Long, V>

I also changed calls to class - I assume that was a typo.

In response to your comment:

MyTask<T, V extends Model>

Here, MyClass is declaring the type parameters T and V. When type parameters are declared they can optionally be bounded with extends. T isn't bounded - it can be any reference type. V has an upper bound of Model. - it must be some type that is or extends Model.

extends AsyncTask<T, Long, V>

Like any declaration for a class not extending Object, this is saying that MyTask extends AsyncTask - I'm sure you understand that much. AsyncTask has three type parameters: in its declaration they are called Params, Progress, and Result. Here, MyTask is providing those type parameters with type arguments - T, Long, and V.

So MyTask is keeping Params as an unbounded type parameter, resolving Progress with the concrete type Long, and bounding Result with Model.

See the Java Tutorials for a good introduction to generics. Then see Angelika Langer's Generics FAQ for further questions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top