我有2個抽象類是被擴展,並使用它們其他類:Java泛型類型不兼容
-----基地-----
public abstract class HttpRequest<RESPONSE_TYPE extends HttpResponse>
{...}
public abstract class HttpResponse<DATA_TYPE>
{...}
-----擴展-----
public class MockyTextRequest extends HttpRequest<MockyTextResponse>
{...}
public class MockyTextResponse extends HttpResponse<TextWithTitle>
{...}
---- ------實用
public class HttpClient
{...
public <REQUEST_TYPE extends HttpRequest<RESPONSE_TYPE>, RESPONSE_TYPE extends HttpResponse<RESULT_TYPE>, RESULT_TYPE> RESPONSE_TYPE synchronicRequest(@NonNull final REQUEST_TYPE httpRequest, @Nullable final Object tag) throws IOException, HttpException, ParseException
{...}
...}
當我編譯,我得到:
error: incompatible types: inference variable RESPONSE_TYPE has incompatible upper bounds HttpResponse,MockyTextResponse
上
MockyTextResponse httpResponse = HttpClient.get().synchronicRequest(new MockyTextRequest(), null);
什麼想法?
施坦格事實上,這個調用
public <REQUEST_TYPE extends HttpRequest<RESPONSE_TYPE>, RESPONSE_TYPE extends HttpResponse<RESULT_TYPE>, RESULT_TYPE> void request(@NonNull final REQUEST_TYPE httpRequest, @Nullable final Object tag, @Nullable final Listener<REQUEST_TYPE, RESPONSE_TYPE> listener)
{...}
稱爲
HttpClient.get().request(new MockyTextRequest(), MainActivity.this);
這裏工作確定
全部代碼github
你真的需要裏面''sychronicRequest' RESULT_TYPE'?順便問一下你在編譯什麼版本的Java? – Radiodef
這是Java 7.我在函數內部使用RESULT_TYPE來避免投射 – Cheborra
嗯,我可能會想出一個聰明的解決方案,但我會說不要屏住呼吸。這看起來像是一個推理錯誤,Java 7充滿了這樣的小事情。 – Radiodef