Java接口层次结构及其有界类型泛型的方法

我有一个场景,发现要建立如下类型的层次结构:

IComponent(接口)-> AComponent(类)

在这里,我希望IComponent包含exeucte()方法,该方法将由实现此接口的所有类实现。现在,除了组件层次结构之外,我还想要所有组件的请求/响应格式,所以我想到了以下内容:

IComponentRequest-> AComponentRequest

IComponentResponse-> IComponentResponse

现在,我想以这样一种方式在IComponent中声明execute()方法,以便所有要实现的类都可以遵循上述请求/响应层次结构的请求和响应类。就代码而言,我目前声明的方法如下:

public interface IComponent<IComponentRequest,IComponentResponse> {

    IComponentResponse execute(IComponentRequest request);
}

我真正想要的是:

<Any type which implements IComponentResponse> execute(<Any type which implements IComponentRequest> request) {
}

我尝试使用如下通用名称:

public interface IComponent<REQUEST,RESPONSE> {} 

但是在这些情况下,在调用方法并接收到REQUEST和RESPONSE不是任何特定Java类型的响应时,我在实现中遇到问题。因此,我再次继续使用适当的接口IComponentRequest和IComponentResponse,但在那里我无法应用有界的泛型。

有人可以帮忙我在这里想念的东西吗?

gr364212028 回答:Java接口层次结构及其有界类型泛型的方法

指定通用名称时,还可以指定类型信息。在这里,您想为每种适当的类型使用extends。像

public interface IComponent<REQUEST extends IComponentRequest,RESPONSE extends IComponentResponse> {
    RESPONSE execute(REQUEST request);
}
本文链接:https://www.f2er.com/2686848.html

大家都在问