Java泛型类型转换

Lio*_*ion 3 java

这是示例代码:

public static BaseListAdapter<? extends Item> getListAdapter() {
    ...
}

...

public class MyClass<T extends Item> {
    ...
    BaseListAdapter<T> adapter = (BaseListAdapter<T>) getListAdapter();
    ...
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨有一个未经检查的强制转换.

java: unchecked cast
  need: BaseListAdapter<T>
  found:    BaseListAdapter<capture#1, ? extends Item>
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会产生这个警告以及如何解决它.

Thi*_*ilo 6

你可能想要

public static <T extends Item> BaseListAdapter<T> getListAdapter() {
Run Code Online (Sandbox Code Playgroud)