麻烦老师查看是否有优化的项

麻烦老师查看是否有优化的项

部门类

package com.self.model;

public class Dept {
	// 部门编号
	private String deptNo;
	// 部门名称
	private String deptName;
	// 部门员工
	private People[] peopleArray;
	// 部门员工个数
	private int peopleNum;

	public String getDeptNo() {
		return this.deptNo;
	};

	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	};

	/**
	 * 获取人员列表
	 * 
	 * @return 当没有进行初始化的时候,默认初始 200
	 */
	public People[] getPeopleArray() {
		if (this.peopleArray == null)
			this.peopleArray = new People[200];
		return this.peopleArray;
	}

	public void setPeopleArray(People[] peopleArray) {
		this.peopleArray = peopleArray;
	}

	public int getPeopleNum() {
		return peopleNum;
	}

	public void setPeopleNum(int peopleNum) {
		this.peopleNum = peopleNum;
	}

	// 无参构造函数
	public Dept() {

	};

	// 带参构造函数
	public Dept(String deptNo, String deptName) {
		this.setDeptNo(deptNo);
		this.setDeptName(deptName);
	}

	// 人员和部门关联信息
	public void addUserDept(People peo) {
		// 循环列表
		for (int i = 0; i < this.getPeopleArray().length; i++) {
			// 判断是否有空位
			if (this.getPeopleArray()[i] == null) {
				// 关联部门和人员的信息
				peo.setDeptObj(this);
				// 赋值人员信息
				this.getPeopleArray()[i] = peo;
				// 累加人员的有效数
				this.setPeopleNum(i + 1);
				// 退出循环
				return;
			}
			;
		}
	}

	// 打印部门的列表信息
	public String deptInfo() {
		// 初始化员工信息
		String userNameInfo = "";
		// 信息返回
		String str = "";
		// 循环列表
		for (int i = 0; i < this.getPeopleNum(); i++) {
			// 累加员工名字
			if (i < this.getPeopleNum() - 1) {
				userNameInfo += this.getPeopleArray()[i].getName() + ",";
			} else {
				userNameInfo += this.getPeopleArray()[i].getName();
			}
		};
		System.out.println("=================================");
		// 显示部门信息
		str = "部门编号:" + this.getDeptNo() + "\n部门名称:" + this.getDeptName() + "\n员工数组:" + userNameInfo + "\n员工人数:"
				+ this.getPeopleNum();
		// 返回部门信息
		return str;
	}

}


职务类

package com.self.model;

public class Duties {
	// 职务编号
	private String dutiesNo;
	// 职务名称
	private String dutiesName;
	// 关联职务
	private People[] dutiesArray;
	// 关联职务人员
	private int dutiesNum;

	public String getDutiesNo() {
		return this.dutiesNo;
	};

	public void setDutiesNo(String dutiesNo) {
		this.dutiesNo = dutiesNo;
	}

	public String getDutiesName() {
		return dutiesName;
	}

	public void setDutiesName(String dutiesName) {
		this.dutiesName = dutiesName;
	};

	public People[] getDutiesArray() {
		if (this.dutiesArray == null)
			this.dutiesArray = new People[200];
		return this.dutiesArray;
	}

	public void setDutiesArray(People[] dutiesArray) {
		this.dutiesArray = dutiesArray;
	}

	public int getDutiesNum() {
		return dutiesNum;
	}

	public void setDutiesNum(int dutiesNum) {
		this.dutiesNum = dutiesNum;
	}

	// 无参构造函数
	public Duties() {

	};

	// 带参构造函数
	public Duties(String dutiesNo, String dutiesName) {
		this.setDutiesNo(dutiesNo);
		this.setDutiesName(dutiesName);
	}

	// 人员和部门关联信息
	public void addUserDuties(People peo) {
		// 循环列表
		for (int i = 0; i < this.getDutiesArray().length; i++) {
			// 判断是否有空位
			if (this.getDutiesArray()[i] == null) {
				// 关联部门和人员的信息
				peo.setDutiesObj(this);
				// 赋值人员信息
				this.getDutiesArray()[i] = peo;
				// 累加人员的有效数
				this.setDutiesNum(i + 1);
				// 退出
				return;
			}
			;
		}
	}

	// 打印职务的列表信息
	public String dutiesInfo() {
		// 初始化员工信息
		String userNameInfo = "";
		// 信息返回
		String str = "";
		// 循环列表
		for (int i = 0; i < this.getDutiesNum(); i++) {
			// 累加员工名字
			if (i < this.getDutiesNum() - 1) {
				userNameInfo += this.getDutiesArray()[i].getName() + ",";
			} else {
				userNameInfo += this.getDutiesArray()[i].getName();
			}
		}
		;
		System.out.println("=================================");
		// 显示部门信息
		str = "职务编号:" + this.getDutiesNo() + "\n职务名称:" + this.getDutiesName() + "\n员工数组:" + userNameInfo + "\n员工人数:"
				+ this.getDutiesNum();
		// 返回部门信息
		return str;
	}

}


员工类

package com.self.model;

public class People {
	// 员工姓名
	private String name;
	// 员工工号
	private String peopleNo;
	// 员工年龄
	private int age;
	// 性别
	private String sex;
	// 部门
	private Dept deptObj;
	// 职务
	private Duties dutiesObj;

	public String getName() {
		return this.name;
	};

	public void setName(String name) {
		this.name = name;
	};

	public String getPeopleNo() {
		return peopleNo;
	};

	public void setPeopleNo(String peopleNo) {
		this.peopleNo = peopleNo;
	};

	public String getSex() {
		return this.sex;
	};

	/**
	 * 设置性别
	 * 
	 * 性别只能是"男"或"女", 反之则设置默认为"男"
	 * 
	 * @param age
	 */
	public void setSex(String sex) {
		if (!(sex.equals("男") || sex.equals("女"))) {
			this.sex = "男";
		} else {
			this.sex = sex;
		}
	};

	public int getAge() {
		return age;
	};

	/**
	 * 设置年龄
	 * 
	 * 限定年龄只能是18-65之间,反之则默认为18岁
	 * 
	 * @param age
	 */
	public void setAge(int age) {
		if (age < 18 || age > 65) {
			this.age = 18;
		} else {
			this.age = age;
		}
	};

	/**
	 * 当没有进行部门赋值的时候,默认赋值部门信息
	 * 
	 * @return 返回部门对象
	 */
	public Dept getDeptObj() {
		if (this.deptObj == null)
			this.deptObj = new Dept();
		return this.deptObj;
	};

	public void setDeptObj(Dept deptObj) {
		this.deptObj = deptObj;
	};

	/**
	 * 当没有进行职务赋值的时候,默认职务职务信息
	 * 
	 * @return 返回部门对象
	 */
	public Duties getDutiesObj() {
		if (this.dutiesObj == null)
			this.dutiesObj = new Duties();
		return dutiesObj;
	}

	public void setDutiesObj(Duties dutiesObj) {
		this.dutiesObj = dutiesObj;
	}

	// 无参构造函数
	public People() {

	}
	
	/**
	 * 赋值个人信息
	 * 
	 * @param name
	 * @param peopleNo
	 * @param age
	 * @param sex
	 * @param deptObj
	 * @param dutiesObj
	 */
	public People(String name, String peopleNo, int age, String sex) {
		this.setName(name);
		this.setPeopleNo(peopleNo);
		this.setAge(age);
		this.setSex(sex);
	};

	/**
	 * 赋值个人信息
	 * 
	 * @param name
	 * @param peopleNo
	 * @param age
	 * @param sex
	 * @param deptObj
	 * @param dutiesObj
	 */
	public People(String name, String peopleNo, int age, String sex, Dept deptObj, Duties dutiesObj) {
		this.setName(name);
		this.setPeopleNo(peopleNo);
		this.setAge(age);
		this.setSex(sex);
		this.setDeptObj(deptObj);
		this.setDutiesObj(dutiesObj);
	};

	public String peopleInfo() {
		System.out.println("=================================");
		String str = "姓名:" + this.getName() + "\n工号:" + this.getPeopleNo() + "\n性别:" + this.getSex() + "\n年龄:"
				+ this.getAge() + "\n部门:" + this.getDeptObj().getDeptName() + "\n职务:"
				+ this.getDutiesObj().getDutiesName();
		return str;
	};

}


主方法测试类

package com.self.Test;

import com.self.model.Dept;
import com.self.model.Duties;
import com.self.model.People;

public class Test {

	public static void main(String[] args) {
		
		// 初始化部门
		Dept dept1 = new Dept("D001", "人事部");
		Dept dept2 = new Dept("D002", "市场部");
		
		// 初始化职务
		Duties dut1 = new Duties("P001", "经理");
		Duties dut2 = new Duties("P002", "助理");
		Duties dut3 = new Duties("P003", "职员");
		
		// 创建对象
		People peo1 = new People("张铭", "S001", 29, "男");
		// 关联部门
		dept1.addUserDept(peo1);
		// 关联职务
		dut1.addUserDuties(peo1);
		// 调用人员信息
		System.out.println(peo1.peopleInfo());
		
		// 创建对象
		People peo2 = new People("李艾爱", "S002", 21, "女");
		// 关联部门
		dept1.addUserDept(peo2);
		// 关联职务
		dut2.addUserDuties(peo2);
		// 调用人员信息
		System.out.println(peo2.peopleInfo());
		
		// 创建对象
		People peo3 = new People("孙超", "S003", 29, "男");
		// 关联部门
		dept1.addUserDept(peo3);
		// 关联职务
		dut3.addUserDuties(peo3);
		// 调用人员信息
		System.out.println(peo3.peopleInfo());
		
		// 创建对象
		People peo4 = new People("张美美", "S004", 26, "女");
		// 关联部门
		dept2.addUserDept(peo4);
		// 关联职务
		dut3.addUserDuties(peo4);
		// 调用人员信息
		System.out.println(peo4.peopleInfo());
		
		// 创建对象
		People peo5 = new People("蓝迪", "S005", 37, "男");
		// 关联部门
		dept2.addUserDept(peo5);
		// 关联职务
		dut1.addUserDuties(peo5);
		// 调用人员信息
		System.out.println(peo5.peopleInfo());
		
		// 创建对象
		People peo6 = new People("米莉", "S006", 24, "女");
		// 关联部门
		dept2.addUserDept(peo6);
		// 关联职务
		dut3.addUserDuties(peo6);
		// 调用人员信息
		System.out.println(peo6.peopleInfo());
		
		// 打印部门信息
		System.out.println(dept1.deptInfo());
		System.out.println(dept2.deptInfo());
		
		
		// 打印职务信息
		System.out.println(dut1.dutiesInfo());
		System.out.println(dut2.dutiesInfo());
		System.out.println(dut3.dutiesInfo());
	}

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
ImoocZhang 2025-09-09 09:43:46

同学你好,你这个问题问的太宏观,笼统了, 具体优化点,要看具体目标以及相应的知识储备。目标不同,优化的点和优化的程度有会有很大差别。下面给你一些简单的优化建议,抛砖引玉下。

一、结构层面:数组 → 集合

问题:固定长度数组 People[200],浪费内存,扩容困难。

优化:用 List<People> 替代数组,自动扩容,无需计数器。

二、设计层面:双向关联 → 单向聚合

问题:People 里持有 DeptDutiesDept/Duties 又维护 People[];造成双向依赖,循环引用,内存泄漏风险。

优化:只让 People 持有 DeptDutiesDept/Duties 不再维护人员列表,只提供查询方法;如果需要“部门下有多少人”,通过外部服务类查询(如 OrgService)。

三、性能层面:线性查找 → Map 索引

问题:addUserDept/addUserDuties 都是 O(n) 线性扫描;员工多时性能差。

优化:用 Map<String, People> 做工号索引,O(1) 查找;若需“部门→人员”反向索引,可建 Map<String, List<People>>

四、代码风格:冗余、魔法值

问题:魔法值 200"男"1865 硬编码;getDeptObj() 里隐式新建对象,隐藏副作用;peopleInfo() 里直接打印,违反单一职责。

优化:用常量或枚举;不隐式新建对象,返回 Optional<Dept>;日志/打印交给 toString() 或工具类。


  • 提问者 ᕦ(ò_óˇ)ᕤ #1

    老师,你好,二、三、四我没有看懂,这个能给出一个具体的例子给我看看吗?

    2025-09-10 21:41:37
  • ImoocZhang 回复 提问者 ᕦ(ò_óˇ)ᕤ #2

    没看懂,暂时不用研究这些了,可以接着继续后面的课程内容,本身代码的优化是随着个人编程经验的丰富才能体会到,做为入门阶段,目前这些内容超出了这门课程的教学范围,也没必要深入到自己看不懂的层面,导致学习不顺畅等问题。

    2025-09-22 11:34:14
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
计算机基础课
  • 参与学习       244    人
  • 解答问题       162    个

1000位程序员+大厂HR联袂推荐,面向所有程序员的计算机核心知识体系,优惠中~

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师