| 编写程序展示一个学校院系结构:要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系 |

| OrganizationComponent可以是接口、抽象类、具体的类 |
| OrganizationComponent聚合到University |
| OrganizationComponent聚合到College |
| |
| package com.atguigu.composite; |
| public abstract class OrganizationComponent { |
| |
| private String name; |
| |
| private String des; |
| |
| protected void add(OrganizationComponent organizationComponent) { |
| |
| throw new UnsupportedOperationException(); |
| } |
| |
| protected void remove(OrganizationComponent organizationComponent) { |
| |
| throw new UnsupportedOperationException(); |
| } |
| |
| |
| public OrganizationComponent(String name, String des) { |
| super(); |
| this.name = name; |
| this.des = des; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getDes() { |
| return des; |
| } |
| |
| public void setDes(String des) { |
| this.des = des; |
| } |
| |
| |
| protected abstract void print(); |
| |
| } |
| |
| package com.atguigu.composite; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class University extends OrganizationComponent { |
| |
| List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); |
| |
| |
| public University(String name, String des) { |
| super(name, des); |
| |
| } |
| |
| |
| @Override |
| protected void add(OrganizationComponent organizationComponent) { |
| |
| organizationComponents.add(organizationComponent); |
| } |
| |
| |
| @Override |
| protected void remove(OrganizationComponent organizationComponent) { |
| |
| organizationComponents.remove(organizationComponent); |
| } |
| |
| @Override |
| public String getName() { |
| |
| return super.getName(); |
| } |
| |
| @Override |
| public String getDes() { |
| |
| return super.getDes(); |
| } |
| |
| |
| @Override |
| protected void print() { |
| |
| System.out.println("--------------" + getName() + "--------------"); |
| |
| for (OrganizationComponent organizationComponent : organizationComponents) { |
| organizationComponent.print(); |
| } |
| } |
| |
| } |
| |
| package com.atguigu.composite; |
| import java.util.ArrayList; |
| import java.util.List; |
| public class College extends OrganizationComponent { |
| |
| |
| List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); |
| |
| |
| public College(String name, String des) { |
| super(name, des); |
| |
| } |
| |
| |
| @Override |
| protected void add(OrganizationComponent organizationComponent) { |
| |
| |
| organizationComponents.add(organizationComponent); |
| } |
| |
| |
| @Override |
| protected void remove(OrganizationComponent organizationComponent) { |
| |
| organizationComponents.remove(organizationComponent); |
| } |
| |
| @Override |
| public String getName() { |
| |
| return super.getName(); |
| } |
| |
| @Override |
| public String getDes() { |
| |
| return super.getDes(); |
| } |
| |
| |
| @Override |
| protected void print() { |
| |
| System.out.println("--------------" + getName() + "--------------"); |
| |
| for (OrganizationComponent organizationComponent : organizationComponents) { |
| organizationComponent.print(); |
| } |
| } |
| |
| } |
| |
| package com.atguigu.composite; |
| public class Department extends OrganizationComponent { |
| |
| |
| |
| public Department(String name, String des) { |
| super(name, des); |
| |
| } |
| |
| |
| |
| @Override |
| public String getName() { |
| |
| return super.getName(); |
| } |
| |
| @Override |
| public String getDes() { |
| |
| return super.getDes(); |
| } |
| |
| @Override |
| protected void print() { |
| |
| System.out.println(getName()); |
| } |
| |
| } |
| |
| package com.atguigu.composite; |
| public class Client { |
| |
| public static void main(String[] args) { |
| |
| OrganizationComponent university = new University("清华大学", " 中国顶级大学 "); |
| |
| |
| OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 "); |
| OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 "); |
| |
| |
| computerCollege.add(new Department("软件工程", " 软件工程不错 ")); |
| computerCollege.add(new Department("网络工程", " 网络工程不错 ")); |
| computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 ")); |
| |
| infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 ")); |
| infoEngineercollege.add(new Department("信息工程", " 信息工程好学 ")); |
| |
| |
| university.add(computerCollege); |
| university.add(infoEngineercollege); |
| |
| |
| infoEngineercollege.print(); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-08-31 vue开发:前端项目模板