例子
将下图关系表示出来
public abstract class Corp { private String name=""; //每个人都有名字 private String position=""; //每个人都有职位 private int salary=0; //每个人都有薪水 public Corp(String _name,String _position,int _salary){ this.name=_name; this.position=_position; this.salary=_salary; } public String getInfo(){ String info=""; info="姓名:"+this.name+"\t 薪水:"+this.salary+"\t 职位:"+this.position; return info; } }
将根节点当做树枝节点
public class Branch extends Corp { ArrayList<Corp> subordinateList=new ArrayList<Corp>(); public Branch(String _name, String _position, int _salary) { super(_name, _position, _salary); } /** * 添加一个下属,可能是个小头目,也有可能是个小兵 * @param corp */ public void addSubordinate(Corp corp){ this.subordinateList.add(corp); } /** *我有哪些下属 * @return */ public ArrayList<Corp> getSubordinate(){ return this.subordinateList; } }
public class Leaf extends Corp { public Leaf(String _name, String _position, int _salary) { super(_name, _position, _salary); } }
public class Client { public static void main(String[] args) { Branch ceo=compositeCorpTree(); System.out.println("CEO信息:"+ceo.getInfo()); getTreeInfo(ceo); } /** * 准备数据 * @return */ public static Branch compositeCorpTree(){ Branch root=new Branch("刘大麻子","总经理",10000); Branch developDep = new Branch("刘大瘸子","研发部门经理",10000); Branch salesDep = new Branch("马二拐子","销售部门经理",20000); Branch financeDep = new Branch("赵三驼子","财务部经理",30000); //再把三个小组长产生出来 Branch firstDevGroup = new Branch("杨三乜斜","开发一组组长",5000); Branch secondDevGroup = new Branch("吴大棒槌","开发二组组长",6000); //把所有的小兵都产生出来 Leaf a = new Leaf("a","开发人员",2000); Leaf b = new Leaf("b","开发人员",2000); Leaf c = new Leaf("c","开发人员",2000); Leaf d = new Leaf("d","开发人员",2000); Leaf e = new Leaf("e","开发人员",2000); Leaf f = new Leaf("f","开发人员",2000); Leaf g = new Leaf("g","开发人员",2000); Leaf h = new Leaf("h","销售人员",5000); Leaf i = new Leaf("i","销售人员",4000); Leaf j = new Leaf("j","财务人员",5000); Leaf k = new Leaf("k","CEO秘书",8000); Leaf zhengLaoLiu = new Leaf("郑老六","研发部副经理",20000); //开始组装 //CEO下有三个部门经理和一个秘书 root.addSubordinate(k); root.addSubordinate(developDep); root.addSubordinate(salesDep); root.addSubordinate(financeDep); //研发部经理 developDep.addSubordinate(zhengLaoLiu); developDep.addSubordinate(firstDevGroup); developDep.addSubordinate(secondDevGroup); //看看两个开发小组下有什么 firstDevGroup.addSubordinate(a); firstDevGroup.addSubordinate(b); firstDevGroup.addSubordinate(c); secondDevGroup.addSubordinate(d); secondDevGroup.addSubordinate(e); secondDevGroup.addSubordinate(f); //再看销售部下的人员情况 salesDep.addSubordinate(h); salesDep.addSubordinate(i); //最后一个财务 financeDep.addSubordinate(j); return root; } /** *遍历所有节点 * @param root * @return */ public static void getTreeInfo(Branch root){ ArrayList<Corp> list= root.getSubordinate(); if(list!=null){ for(Corp c:list){ if(c instanceof Leaf){ System.out.println(c.getInfo()); }else{ System.out.println(root.getInfo()); getTreeInfo((Branch) c); } } } } }
CEO信息:姓名:刘大麻子 薪水:10000 职位:总经理 姓名:k 薪水:8000 职位:CEO秘书 姓名:刘大麻子 薪水:10000 职位:总经理 姓名:郑老六 薪水:20000 职位:研发部副经理 姓名:刘大瘸子 薪水:10000 职位:研发部门经理 姓名:a 薪水:2000 职位:开发人员 姓名:b 薪水:2000 职位:开发人员 姓名:c 薪水:2000 职位:开发人员 姓名:刘大瘸子 薪水:10000 职位:研发部门经理 姓名:d 薪水:2000 职位:开发人员 姓名:e 薪水:2000 职位:开发人员 姓名:f 薪水:2000 职位:开发人员 姓名:刘大麻子 薪水:10000 职位:总经理 姓名:h 薪水:5000 职位:销售人员 姓名:i 薪水:4000 职位:销售人员 姓名:刘大麻子 薪水:10000 职位:总经理 姓名:j 薪水:5000 职位:财务人员