/** * 定义公共抽象类,定义规范信息 * @author Administrator * */ public abstract class OrganizationComponent { //学校名称 private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public OrganizationComponent(String name) { this.name = name; } /** * 添加的方法 * @param organizationComponent */ protected void add(OrganizationComponent organizationComponent) { } /** * 删除学校的方法 * @param organizationComponent */ protected void remove(OrganizationComponent organizationComponent) { } /** * 打印信息 */ protected abstract void print(); } /** * 学校的信息 * @author Administrator * */ public class University extends OrganizationComponent{ List<OrganizationComponent> lists = new ArrayList<OrganizationComponent>(); public University(String name) { super(name); } @Override protected void add(OrganizationComponent organizationComponent) { lists.add(organizationComponent); } @Override protected void remove(OrganizationComponent organizationComponent) { lists.remove(organizationComponent); } @Override protected void print() { System.out.println("-------------"+this.getName()+"-------------------"); for (OrganizationComponent list : lists) { list.print(); } } } /** * 学院的基本信息 * @author Administrator * */ public class College extends OrganizationComponent{ List<OrganizationComponent> lists = new ArrayList<OrganizationComponent>(); public College(String name) { super(name); } @Override protected void add(OrganizationComponent organizationComponent) { lists.add(organizationComponent); } @Override protected void remove(OrganizationComponent organizationComponent) { lists.remove(organizationComponent); } @Override protected void print() { System.out.println("--" + this.getName() + "--"); for (OrganizationComponent list : lists) { list.print(); } } } /** * 专业信息 * @author Administrator * */ public class Major extends OrganizationComponent { public Major(String name) { super(name); } @Override protected void print() { System.out.println(this.getName()); } } /** * 展示学校的信息 * @author Administrator * */ public class Client { public static void main(String[] args) { OrganizationComponent university = new University("东北大学"); OrganizationComponent college1 = new College("计算机学院"); OrganizationComponent college2 = new College("生物工程学院"); university.add(college1); university.add(college2); OrganizationComponent major1 = new Major("软件工程"); OrganizationComponent major2 = new Major("网络工程"); college1.add(major1); college1.add(major2); OrganizationComponent major3 = new Major("生物工程"); OrganizationComponent major4 = new Major("生物科学"); college2.add(major3); college2.add(major4); university.print(); } } |