接口中的 Java 泛型
我在 Java 中遇到了泛型问题,并且在网上找不到以类似方式使用泛型的解决方案或示例。我有一组具有相同请求/响应结构的方法。例如,用户填充 Request 对象中的字段,将该对象传递给帮助器方法,然后返回 Response 对象。我的所有请求对象都从公共 Request 超类扩展(同样,Response 对象也从 Response 超类扩展)。我希望我的帮助器类也具有一致的结构,因此我使用了一个接口。下面是一些代码来说明...
请求超类:
public class SuperRequest {
// ...
}
示例 请求子类:
public class SubRequest extends SuperRequest {
// ...
}
响应超类:
public class SuperResponse {
// ...
}
示例响应子类:
public class SubResponse extends SuperResponse{
// ...
}
接口:
public interface SomeInterface {
public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);
}
正如您从接口中看到的,我想传递一个作为 子级的对象SuperRequest
并且我想返回一个作为 SuperResponse
子级的对象。这是我的实现:
public class SomeImplementation implements SomeInterface {
@Override
public SubResponse someMethod(SubRequest request) {
return null;
}
}
在 Eclipse 中,我收到编译错误,因为编译器告诉我有未实现的方法,并且 @Override
符号“不重写超类型方法”。
有人可以帮我吗?是我的语法不正确还是我对泛型的理解有点偏差?非常感谢任何帮助!
I have run into a problem with generics in Java and I can't find a solution or example online of someone using generics in a similar fashion. I have a set of methods that are in the same request/response structure. For example, a user populates fields in the Request object, passes the object to a helper method, and they are returned a Response object. All of my request objects extend from a common Request super class (and similarly, Response objects from a Response super class). I would like my helper classes to also have a consistent structure so I have used an interface. Here is some code to illustrate...
Request super class:
public class SuperRequest {
// ...
}
Example Request subclass:
public class SubRequest extends SuperRequest {
// ...
}
Response super class:
public class SuperResponse {
// ...
}
Example response subclass:
public class SubResponse extends SuperResponse{
// ...
}
The interface:
public interface SomeInterface {
public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);
}
As you can see from the interface, I want to pass an object that is a child of SuperRequest
and I want to return an object that is a child of SuperResponse
. Here is my implementation:
public class SomeImplementation implements SomeInterface {
@Override
public SubResponse someMethod(SubRequest request) {
return null;
}
}
In Eclipse, I get compilation errors because the compiler tells me I have unimplemented methods and the @Override
notation "does not override a supertype method".
Can anyone help me here? Is my syntax incorrect or is my understanding of generics a little off? Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将您的接口和实现更改为:
Try changing your interface and implementation to:
按照目前的情况,
SomeInterface
的实现必须实现参数化方法,即:它不能选择特定的子类来替代
T
和U
>。接口本身必须是通用的
SomeInterface
,然后子类可以为T
和U
选择具体实现。As it stands, the implementation of
SomeInterface
must implement the parameterized method, i.e.:It can't choose a particular Subclass to substitute for
T
andU
.The interface itself must be generic
SomeInterface<T extends SuperRequest, U extends SuperResponse>
and the subclass can then choose concrete implementations forT
andU
.实现接口时还必须声明类型。试试这个:
You have to declare the types as well when you implement the interface. Try this: