2011-06-20 40 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

相關問題