首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用泛型进行重构

使用泛型进行重构
EN

Stack Overflow用户
提问于 2012-05-15 23:57:14
回答 3查看 119关注 0票数 2

我习惯于在类型集合中使用泛型,但我从未真正使用过它们来开发某些东西。

我有几个这样的类:

代码语言:javascript
复制
public class LogInfoWsClient extends GenericWsClient {
    public void sendLogInfo(List<LogInfo> logInfoList) {
        WebResource ws = super.getWebResource("/services/logInfo");
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<List<LogInfo>>(logInfoList) {
            });     
    }
}

其中,两者之间唯一变化的是服务字符串("/services/info")和列表的类型(本例中为LogInfo)

我已经将几个方法重构为一个GenericWsClient类,但我的目标是拥有一些可以像这样使用的东西:

代码语言:javascript
复制
List<LogInfo> myList = database.getList();
SuperGenericClient<List<LogInfo>> superClient = new SuperGenericClient<List<LogInfo>>();
superClient.send(myList,"/services/logInfo");

但我不知道该怎么做,甚至不知道它是否可行。有可能吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-16 00:02:23

是的,事实上,如果你查看java.util.collection包,你会发现所有的类都是参数find。

所以你的类应该是这样的

代码语言:javascript
复制
public SuperGenericClient<E> {       
    public E getSomething() {
         return E;
    }
}

然后使用它,你将拥有

代码语言:javascript
复制
SuperGenericClient<String> myGenericClient = new SuperGenericClient<String>();
String something = myGenericClient.getSomething();

扩展您的示例本身,您的代码将如下所示:

代码语言:javascript
复制
public class SuperGenericClient<E> extends GenericWsClient {
    public void send(List<E> entityList, String service) {
        WebResource ws = super.getWebResource(service);
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<E>(entityList) {
            });
        }            
    }
}

public class GenericEntity<E> {
    public GenericEntity(List<E> list){

    }
}

为了更好地理解泛型,您必须阅读this

票数 1
EN

Stack Overflow用户

发布于 2012-05-16 00:03:07

你可以像下面这样写你的类--你可以将同样的想法应用于GenericEntity

代码语言:javascript
复制
public class SuperGenericClient<T> extends GenericWsClient {

    public void send(List<T> list, String service) {
        WebResource ws = super.getWebResource(service);
        try {
            String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, new GenericEntity<T>(list) {
            });
        }            
    }
}

然后你可以这样叫它:

代码语言:javascript
复制
List<LogInfo> myList = database.getList();
SuperGenericClient<LogInfo> superClient = new SuperGenericClient<LogInfo>();
superClient.send(myList,"/services/logInfo");
票数 1
EN

Stack Overflow用户

发布于 2012-05-16 00:03:58

像这样声明你的类:

代码语言:javascript
复制
public class LogThing<T> {
    public void sendLogInfo(List<T> list) {
        // do thing!
    }
}

当你使用它的时候,像这样做:

代码语言:javascript
复制
List<LogInfo> myList = db.getList();
LogThing<LogInfo> superClient = new LogThing<LogInfo>();
superClient.sendLogInfo(myList);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10604504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档