2011-06-20 89 views
3

我试图存储数据类型为DataType<? extends T>的变量。
我试过DataType<? extends T> var;但它似乎不工作。在类字段中存储数据类型<? extends T>

作为DataType<?> var;存储作品,但我不能投到DataType<? extends T>

有没有可能让它工作?

编辑:

也许会更容易,当我提供更多的信息。

我使用AndroidHttpClient的AsyncTask,而呈现出ProgressDialog,其执行在背景不同的请求。
我正在寻找一个简单的实现,这可能会允许我传输ResponseHandler作为HttpClient实现的方法execute的参数。

+2

你能添加一些包含datamember声明的代码,以及类的泛型声明吗? – wolfcastle

+0

这是[接口](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ResponseHandler.html),这是[方法](http: //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html#execute%28org.apache.http.client.methods.HttpUriRequest,%20org.apache.http .client.ResponseHandler%29) – CSchulz

回答

2

EDIT

问题是参数化的类型是在该方法中声明。您不能将参数存储为类数据成员所需的类型,因为在类声明中无法知道类型,因为只有在调用方法时才会确定类型信息。

public class Snippet<T> { 

    private final ResponseHandler<? extends T> var; 

    public Snippet(ResponseHandler<? extends T> var) { 
     super(); 
     this.var = var; 
    } 


    public <U> U execute(ResponseHandler<? extends U> responseHandler) { 
     // This class is generic wrt to T, but this method is generice wrt to U. 
     // You cannot store the variable passed in here in a data member 
     // because the type cannot possible be known at compile time, as it 
     // depends on client code calling this method. 
     return null; 
    } 
} 
+0

不错的片段,但我不知道T.看到我上面的评论。 – CSchulz

+0

@ H3llGhost这是_Exactly_问题。你不/不知道T(或者在我的例子中是U),所以在维护类型信息的时候没有办法维护它。所以你必须回落到。我仍然100%不清楚你真正想做什么,但我怀疑这是不可能的。 – wolfcastle

+0

''不工作,因为'execute(HttpHost target,HttpRequest request,ResponseHandler responseHandler)'方法需要* ResponseHandler *和''。 – CSchulz

相关问题