6
回答
我自己写的一个JAVA泛型问题
public class Tree<T> {
private T self;
private List<T> childs;
public T getSelf() {
return self;
}
public void setSelf(T self) {
this.self = self;
}
public List<T> getChilds() {
return childs;
}
public void setChilds(List<T> childs) {
this.childs = childs;
}
} 
在一个DAO实现里面用这个泛型类
public List<Tree> buildRoleTreeF(List<Role> list) {
List<Tree> Trees=new ArrayList<Tree>();
List<Role> roots=findAllRoots(list);
if(roots.size()>0){
for(Role root:roots){
Tree<Role> Tree=new Tree<Role>();
Tree.setSelf(root);
List<Role> childs=findAllChilds(root,list);
Tree.setChilds(childs);
Trees.add(Tree);
}
return Trees;
}else{
return null;
}
}
List<Role> list=bean.getList();
List<Tree> roleTrees=bean.buildRoleTreeF(list);
for(Tree rt:roleTrees){
System.out.println(rt.getSelf());
} 
为什么rt.getSelf取出来的时候还是要转型呢,泛型存进去的时候指定的类型不是不用转了么?
举报
顶部