2011-06-20 7 views
3

データタイプがDataType<? extends T>の変数を保存しようとしています。
私はDataType<? extends T> var;で試しましたが、うまくいかないようです。クラスデータフィールドに<? extends T>を格納

DataType<?> var;として保存していますが、DataType<? extends T>にキャストできません。

動作させる可能性はありますか?

編集:私はより多くの情報を与えるとき

おそらくそれは、容易になります。

Iは、ProgressDialogを示しながら、バックグラウンドで異なる要求を実行AsyncTaskAndroidHttpClientを使用します。
HttpClient実装のexecuteメソッドのパラメータとしてResponseHandlerを転送できるようにする簡単な実装を探しています。

+2

datamember宣言とクラスジェネリック宣言を含むコードを追加できますか? – wolfcastle

+0

これは[Interface](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それは正確に問題です。あなたはT(または私の例ではU)を知らない/知ることができないので、型情報を維持しながらハンドルを維持する方法はありません。だから、に戻る必要があります。私はあなたが本当にやりたいことについてまだ100%はっきりしていませんが、私はそれが不可能であるという疑いがあります。 ''で* ResponseHandler *が必要であるため、 – wolfcastle

+0

''は動作しません。これは、 'execute(HttpHost target、HttpRequest request、ResponseHandler responseHandler) – CSchulz

関連する問題