我有一个类将包含要发送到UI的响应。蓝图是:类中的嵌套通用多态返回类型
class Response<T extends CustomObject> {
private String var1;
private String var2;
private List<T> custom; (eg. customOne, customTwo)
}
我能有这样的正在扩大CustomObject并根据该响应类将有不同的customObject列表自定义对象不同的对象。
应用服务逻辑后,我得到一个原始响应,并基于自定义对象尝试以不同方式进行解析。
CusomOne和CustomTwo将具有不同的结构为:
class CustomOne extends CustomObject {
private String v1;
}
class CustomTwo extends CustomObject {
private String v2;
}
我有一个抽象的解析函数将基于该被拾取的对象被调用。该功能定义为:
public abstract ResponsePayLoad<? extends CustomObject> parseResponse(String response);
ReponsePayLoad是另一个具有其他字段(包括CustomObject)的类。类ResponsePayLoad的蓝图是:
class ResponsePayLoad<T extends CustomObject> {
private String varX;
private List<T> value;
}
两个customObjects解析功能将是这样的:
public ResponsePayLoad<customOne> parseResponse(String response){
CustomOne one = ; // parsingLogic
return one;
}
public ResponsePayLoad<customTwo> parseResponse(String response){
CustomTwo two = ; // parsingLogic
return two;
}
在我的业务逻辑,当我写的代码如下:
ResponsePayLoad<CustomObject> responseObj = parseResponse(response);
我需要将它转换为我不想要的ResponsePayLoad。
任何人都可以告诉我如何跳过使用“?”在抽象函数中仍然保持相同的逻辑?此外,我不想像上面定义的那样类型化。任何帮助,将不胜感激。