当前位置: 代码迷 >> java >> Java通用类型限制类类型
  详细解决方案

Java通用类型限制类类型

热度:72   发布时间:2023-07-17 20:39:28.0

我正在为服务编写客户端包装。 哪些已经给客户端实现。 具有两个类的层次结构:

  1. 帐户
  2. 产品

这两个类不共享公共的父接口。 但是他们在使用它们时具有类似的实现:

一段代码执行的例子

帐户服务需要以下代码步骤:

 final AccountsCustomBatchRequest content = new AccountsCustomBatchRequest();
 content.setEntries(request);
 final AccountsCustomBatchResponse response =   this.service.accounts().custombatch(content).setDryRun(this.isDryRun)
                        .execute();
return response.getEntries();

产品类似:

final ProductsCustomBatchRequest content = new ProductsCustomBatchRequest();
 content.setEntries(request);
 final ProductsCustomBatchResponse response =   this.service.products().custombatch(content).setDryRun(this.isDryRun)
                        .execute();
return response.getEntries(); 

有没有办法概括代码? 我不能使用通用模式,因为它们都不支持公共父类。 我可以喜欢:

   T extends Account or Product ?

他们共享这种方法: custombatch(content)

我将创建一个包含custombatch方法的接口,并进行类似T extends custombatchInterface

如果两个类不共享相同的层次结构,则无法将它们组合在一起。 一个可能的方法是创建一个新的接口CustomBatchInterface与方法custombatch(content) ,并创建两个classes ,其extendsProductsCustomBatchResponseAccountsCustomBatchResponseimplements您的CustomBatchInterface

  相关解决方案