我定义了以下类:
class Operation<S>
class GetReservationOperation extends Operation<Reservation>
现在我想要一个这样的课程:
OperationExecutor<T extends Operation<S>> extends AsyncTask<T,Void,S>{
@Override
protected S doInBackground(T... params) {
return null;
}
}
但这不会编译:
OperationExecutor<GetReservationOperation> executor = new ....
为什么Java不允许这样做?
一段时间后,我想出了以下解决方案:
OperationExecutor<T extends Operation<S>,S> extends AsyncTask<T,Void,S>{
@Override
protected S doInBackground(T... params) {
return null;
}
}
但这迫使我写下以下内容:
OperationExecutor<GetReservationOperation,Reservation> executor = new ....
这看起来很奇怪。有什么办法让它看起来更好看吗?
编辑 这工作
OperationExecutor<S> extends AsyncTask<Operation<S>,Void,S>{
@Override
protected S doInBackground(Operation<S>... params) {
return null;
}
}
OperationExecutor<Reservation> executor = new ....
executor.execute(getReservationOperation);