首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

关于泛型。该如何解决

2013-11-29 
关于泛型。类KerryPagination:public class KerryPagination{privateListT listShow new ArrayListT(

关于泛型。
类KerryPagination:
public class KerryPagination{
     private  List<T> listShow = new ArrayList<T>();
     public KerryPagination(List<T> gg) {
this.listShow = gg ;
}
}
在类PaginationBean中实例化KerryPagination:
List<TGameList> gg = new ArrayList<TGameList>();
KerryPagination kerry = new KerryPagination(gg);

请问下这样写哪里有问题?要怎么写
[解决办法]
List<T> gg = new ArrayList<T>();
KerryPagination kerry = new KerryPagination(gg);
需要的是List里包含的是<T>的数组啊,你就得传个包含T的过去
[解决办法]
那你就别用泛型了,有必要的话根据需求强制转换
[解决办法]
public class KerryPagination{
     private  List<?> listShow = new ArrayList<?>();
     public KerryPagination(List<T> gg) {
this.listShow = gg ;
}
}

[解决办法]
public class KerryPagination<T>{
     private  List<?> listShow = new ArrayList<?>();
     public KerryPagination(List<T> gg) {
this.listShow = gg ;
}
}
这样写
[解决办法]
List<TGameList> gg = new ArrayList<TGameList>();
KerryPagination kerry = new KerryPagination<TGameList>(gg);
[解决办法]

引用:
Quote: 引用:

public class KerryPagination<T>{
     private  List<?> listShow = new ArrayList<?>();
     public KerryPagination(List<T> gg) {
this.listShow = gg ;
}
}
这样写

private  List<?> listShow = new ArrayList<?>();
这样写会报错的

private  List<?> listShow ;或者private  List<?> listShow = null;


[解决办法]
public class KerryPagination<T>{
private  List<T> listShow=new ArrayList<T>()  ;
    public KerryPagination(List<T> gg) {
    this.listShow = gg ;
}
}

我试了一下 这样是可以的,你可以看看API中,泛型类都是怎么写的。
Comparable 和comparator都比较有代表性

热点排行