你如何传递一个List<实现接口的对象>一个方法? [英] How can you pass a List<objects that implement an interface> to a method?

查看:191
本文介绍了你如何传递一个List<实现接口的对象>一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个servlet,通过查询参数传递,从DAO获取对象列表,将列表转换为JSON,并将其发回给响应。每个列表由具有方法的对象组成:

  public String getAsJson(){...} 

而且这个servlet有一堆大多数简单的方法,如下所示:

  private String getUserListAsJson(){
List< User> userList = this.dao.getUsers();
StringBuilder builder = new StringBuilder();
builder.append('[');
//在列表中循环添加每个对象的值getAsJson()
builder.append(']');
return builder.toString();
}

问题是我有大约6种方法(和增长)除了不同的DAO查询之外。我的想法是创建一个只有getAsJson()方法定义的接口,让每个bean实现它,然后在servlet中使用实现了该接口的对象的另一个方法。最后看起来像这样:

  public interface JsonEnabled {
public String getAsJson();

$ b $ public class User实现JsonEnabled {
....
@Override
public String getAsJson(){...}
}

public class TheServlet {
...
private String getUserListAsJson(){
List< User> userList = this.dao.getUsers();
返回this.getListAsJson(userList);
}
private String getListAsJson(List< ;? implements JsonEnabled> list){
//每个方法中的循环代码。






$ b因此如果有人真的读过这个= P,不能编译,并且在查看Oracle的一些文档后,您只能对通用参数进行扩展,而不是实现。使所有的类从一个只有getAsJson()方法的抽象类扩展到语义上没有意义(类是无关的)。一个很好的解决方案,或者只是搜索一下,所以任何帮助/洞察力将不胜感激。

对于通用通配符关键字 extends 适用于类和接口:

  private String getListAsJson(List< ?extends JsonEnabled> list){...} 

extends 在用于定义泛型边界时具有稍微不同的含义 - 它本质上转换为是,或扩展或实现。


I have a servlet that, passed on query params, gets a list of objects from the DAO, turns the list into JSON, and sends it back in the response. Every list is made of objects that have a method:

public String getAsJson(){...}

And the servlet has a bunch of mostly indentical methods that look like:

private String getUserListAsJson() {
    List<User> userList = this.dao.getUsers();
    StringBuilder builder = new StringBuilder();
    builder.append('[');
    // loops over the list appending the value of each objects getAsJson()
    builder.append(']');
    return builder.toString();
}

The problem is that I have about 6 methods (and growing) that look exactly like that except for different DAO queries. My idea was to create an interface that only had the definition for the getAsJson() method, make each bean implement that, and then have another method in the servlet that took objects that implemented that interface. Ended up looking like this:

public Interface JsonEnabled {
    public String getAsJson();
}

public class User implements JsonEnabled {
    ....
    @Override
    public String getAsJson() {...}
}

public class TheServlet {
    ...
    private String getUserListAsJson() {
        List<User> userList = this.dao.getUsers();
        return this.getListAsJson(userList);
    }
    private String getListAsJson(List<? implements JsonEnabled> list) {
        // The loop code that is in each method.
    }
}

So if anyone has actually read this far =P, that doesn't compile and after looking up some documentation from Oracle, you can only have extends and not implements for generic parameters. Making all the classes extend from an Abstract Class that just has the getAsJson() method doesn't make sense semantically (the classes are unrelated).

I haven't found a good solution on SO or just googling around, so any help/insight would be appreciated.

解决方案

For generic wildcards the keyword extends works for both classes and interfaces:

private String getListAsJson(List<? extends JsonEnabled> list) { ... }

extends has slightly different meaning when used for defining generic bounds - it essentially translates to "is, or extends, or implements".

这篇关于你如何传递一个List&lt;实现接口的对象&gt;一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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