接口中的 Java 泛型

发布于 2025-01-07 12:48:57 字数 1185 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

篱下浅笙歌 2025-01-14 12:48:58

尝试将您的接口和实现更改为:

interface SomeInterface<T extends SuperRequest, U extends SuperResponse> {
    public U someMethod(T request);
}

class SomeImplementation implements SomeInterface<SubRequest, SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}

Try changing your interface and implementation to:

interface SomeInterface<T extends SuperRequest, U extends SuperResponse> {
    public U someMethod(T request);
}

class SomeImplementation implements SomeInterface<SubRequest, SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}
薆情海 2025-01-14 12:48:58

按照目前的情况,SomeInterface 的实现必须实现参数化方法,即:

public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);

它不能选择特定的子类来替代 TU >。

接口本身必须是通用的 SomeInterface,然后子类可以为 TU 选择具体实现。

As it stands, the implementation of SomeInterface must implement the parameterized method, i.e.:

public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);

It can't choose a particular Subclass to substitute for T and U.

The interface itself must be generic SomeInterface<T extends SuperRequest, U extends SuperResponse> and the subclass can then choose concrete implementations for T and U.

小霸王臭丫头 2025-01-14 12:48:58

实现接口时还必须声明类型。试试这个:

public class SomeImplementation implements SomeInterface<SubRequest,SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}

You have to declare the types as well when you implement the interface. Try this:

public class SomeImplementation implements SomeInterface<SubRequest,SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文