public class Test {
public void test(){
Demo demo = new Demo();
TextView textView = demo.getView(1);
}
class Demo {
private View ContentView = null;
public <T extends View> T getView(int id){
return ContentView.findViewById(id);
}
}
}
上面没有问题。下面的编译失败,需要强大的墙转换类型
public class Test {
public void test(){
Demo demo = new Demo();
TextView textView = demo.getView(1);
}
class Demo<B> {
private View ContentView = null;
public <T extends View> T getView(int id){
return ContentView.findViewById(id);
}
}
}
我刚刚添加了类的泛型类型,并且在调用getView()时必须强制使用它。为什么?