如何在此有界泛型方法中返回列表? [英] How to return a List inside this bounded generic method?

查看:39
本文介绍了如何在此有界泛型方法中返回列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上课有回音

 公共类Response< T扩展ResponseData>{私人最终T数据;//其他字段,获取器,设置器...} 

和空接口ResponseData:

 公共接口ResponseData {} 

它将任何对象作为数据,但是该对象必须实现空"对象.我创建的界面只是强制所有返回在数据"内部的类.元素具有相同的超类型

现在的问题是,我需要在数据元素内返回一个 List ,但是:

  1. 我发现创建一个 ResponseData 实现仅是我的 List

    的包装是不干净的
  2. 我无法直接返回 List ,因为它不属于我的代码,因此无法使其实现标记接口( ResponseData )

除了我说应该删除标记接口并允许在数据元素中返回任何对象的那个问题之外,还有其他解决方案吗?

nb :我标记器接口的目的是使所有创建的类都将返回到响应内部的数据元素中,以供阅读它们的任何人明确它们的存在目的

n.b.:如上面第1条所述,我知道此解决方案存在:

 类ResponseDataWrapper实现ResponseData {私人最终List< SomeType>responseList;//getters,setters} 

,但不干净,因为在这种情况下, List 和"data"之间没有一层嵌套(即包装类).响应中的元素

解决方案

具有这样的响应对象,该对象返回列表:

  @AllArgsConstructor@NoArgsConstructor@Builder@数据公共类SomeCoolResponseForAnApi实现ResponseData {私有列表< SomeObject>theListYouWantToReturn;@AllArgsConstructor@NoArgsConstructor@Builder@数据公共静态类SomeObject {私有字符串名称;私人弦乐时代;}} 

PS:我使用过龙目岛,您可以只使用常规的吸气剂和吸气剂.

I have class Response

public class Response<T extends ResponseData> {
    private final T data;
    //other fields, getters, setters ...
}

and the empty interface ResponseData:

public interface ResponseData {
}

It takes any object as data but this object must implement the "empty" interface which I created just to force all classes returned inside "data" element be of same super type

Now the problem is that I need to return a List inside the data element, but:

  1. I don't find it clean to create a ResponseData implementation which serves only as a wrapper around my List

  2. I can't return the List directly because it doesn't belong to my code and therefore I can't make it implement the marker interface (ResponseData)

So is there a solution to my problem other than the one that says I should delete the marker interface and allow any object to be returned in the data element?

n.b. : My purpose of the marker interface is to make any created classes which will be returned inside the data element inside the response,for anyone who reads them, clear towards their purpose of existence

n.b. : As mentioned in number 1 above, I know that this solution exists:

class ResponseDataWrapper implements ResponseData{
    private final List<SomeType> responseList;
    
    //getters,setters
}

but it is not clean as in this case there is a layer of nesting (i.e. the wrapper class) which is not necessary between the List and the "data" element in the response

解决方案

Have a response object like this that Returns the list:

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class SomeCoolResponseForAnApi implements ResponseData{
    private List<SomeObject> theListYouWantToReturn;

    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Data
    public static class SomeObject{
        private String name;
        private String age;
    }
}

PS: I have used lombok, you can just use regular getters and setters.

这篇关于如何在此有界泛型方法中返回列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆