[英]Is it possible to have a method signature be a subtype of the interface it implements?
Say I have an interface:假设我有一个界面:
public interface IDto {
}
and a class that implements this interface:和实现此接口的 class :
public class UserProfileResponseDto implements IDto {
}
In another interface I have:在另一个界面中,我有:
public interface ISortingController {
public List<IDto> findAll();
}
In my concrete controller I have:在我的具体 controller 我有:
public List<UserProfileResponseDto> findAll();
but its telling me that it must be但它告诉我它一定是
public List<IDto> findAll();
Is there a way that would allow public List<UserProfileResponseDto> findAll();
有没有办法允许
public List<UserProfileResponseDto> findAll();
as it feels like it should be allowed since UserProfileResponseDto
implements IDto
.因为感觉应该允许,因为
UserProfileResponseDto
实现IDto
。 The reason I ask this question is because I feel that it gives more verbosity the the controller class, which would make it easier to maintain and operate with.我问这个问题的原因是因为我觉得它让 controller class 更加冗长,这将使其更易于维护和操作。
You need to declare a type parameter in your ISortingController
like this:您需要在
ISortingController
中声明一个类型参数,如下所示:
public interface ISortingController<D extends IDto> {
public List<D> findAll();
}
and your controller implementation needs to say implements ISortingController< UserProfileResponseDto>
.并且您的 controller 实现需要说
implements ISortingController< UserProfileResponseDto>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.