是否可以覆盖一个类,但是如果在此方法上直接调用重写方法(而不是在超级接口上),则提供必须提供的特定实现?
class BaseService implements BaseInferface {
//something like this?
@Override //implements the interface
void run(<? extends BaseDTO) dto) {
}
}
class CustomService extends BaseService {
//CustomDTO extends BaseDTO
@Override
void run(CustomDTO dto) {
}
}
我的目标是:
baseService.run(new BaseDTO()); //OK
customService.run(new BaseDTO()); //NOK as I want to make the CustomDTO required
答案 0 :(得分:4)
你可以这样做:
interface BaseInterface<T extends BaseDTO>
{
void run(T dto);
}
class BaseService<T extends BaseDTO> implements BaseInterface<T>
{
}
class CustomService extends BaseService<CustomDTO>
{
}