有人可以帮我解释为什么这种类型不匹配吗?
public interface ResponseContainer<T extends ResponsePayload<T>> {
public T getResponsePayload();
}
public interface ResponsePayload<T> {
public T getPayload();
}
protected abstract <T> Class<? extends ResponseContainer<? extends ResponsePayload<T>>> getResponseClazz(Class<? extends ResponsePayload<T>> responseClazz);
并称之为:
private void tryThis() {
ResponseContainer<AccountsResponse> contaner = getResponseClazz(AccountsResponse.class);
}
给出编译错误:
Type mismatch: cannot convert from Class<capture#22-of ? extends ResponseContainer<? extends ResponsePayload<AccountsResponse>>> to ResponseContainer<AccountsResponse>
这是因为我无法在语法上使用?什么东西实现它而不是扩展它时扩展T?
答案 0 :(得分:2)
您需要更改getResponseClazz
的返回类型。您还应该更改通用定义以避免使用通配符。签名应该是
protected abstract <T extends ResponsePayload<T>> ResponseContainer<T> getResponseContainer(Class<T> responseClazz);
编辑感谢Thilo,方法名称已更改