1700468293
1700468294
public class Branch extends Corp{
1700468295
1700468296
//领导下边有那些下级领导和小兵
1700468297
1700468298
ArrayList<Corp>subordinateList=new ArrayList<Corp>();
1700468299
1700468300
//构造函数是必须的
1700468301
1700468302
public Branch(String_name,String_position,int_salary){
1700468303
1700468304
super(_name,_position,_salary);
1700468305
1700468306
}
1700468307
1700468308
//增加一个下属,可能是小头目,也可能是个小兵
1700468309
1700468310
public void addSubordinate(Corp corp){
1700468311
1700468312
this.subordinateList.add(corp);
1700468313
1700468314
}
1700468315
1700468316
//我有哪些下属
1700468317
1700468318
public ArrayList<Corp>getSubordinate(){
1700468319
1700468320
return this.subordinateList;
1700468321
1700468322
}
1700468323
1700468324
}
1700468325
1700468326
场景类中构建树形结构,并进行遍历。组装没有变化,遍历组织机构数稍有变化,如代码清单21-17所示。
1700468327
1700468328
代码清单21-17 稍稍修改的场景类
1700468329
1700468330
public class Client{
1700468331
1700468332
//遍历整棵树,只要给我根节点,我就能遍历出所有的节点
1700468333
1700468334
public static String getTreeInfo(Branch root){
1700468335
1700468336
ArrayList<Corp>subordinateList=root.getSubordinate();
1700468337
1700468338
String info=””;
1700468339
1700468340
for(Corp s:subordinateList){
1700468341
1700468342
if(s instanceof Leaf){//是员工就直接获得信息
[
上一页 ]
[ :1.700468293e+09 ]
[
下一页 ]