3-8作业中部门员工统计问题
老师好:
这是我的关联代码,由于要统计人事与市场部的员工人数,我用了两个整型变量进行统计:
//完成员工与部门的双向关联 public void addEmployee(Employee emp) { for(int i=0;i<this.getMyEmployee().length;i++) { if(this.getMyEmployee()[i]==null) { emp.setEmployeeDep(this);//为参数emp对象设置部门 this.getMyEmployee()[i]=emp;//部门有哪些员工 if(this.getMyEmployee()[i].getEmployeeDep().getDepartmentNam().equals("人事部")) this.myEmployeeNum+=1;//统计人事部员工人数 if(this.getMyEmployee()[i].getEmployeeDep().getDepartmentNam().equals("市场部")) this.myEmployeeNum1+=1;//统计市场部员工人数 return; } } }
测试类中我用的是员工去统计部门的人数:
public static void main(String[] args) { //实例化部门 Department d1=new Department("部门01","人事部"); Department d2=new Department("部门02","市场部"); //实例化职务 Post p1=new Post("职务01","经理"); Post p2=new Post("职务02","助理"); Post p3=new Post("职务03","职员"); //实例化员工 Employee e1=new Employee("S001","张铭","男",29,d1,p1); Employee e2=new Employee("S002","李艾爱","女",21,d1,p2); Employee e3=new Employee("S004","孙超","男",29,d1,p3); Employee e4=new Employee("S005","张美美","女",26,d2,p3); Employee e5=new Employee("S006","蓝迪","男",37,d2,p1); Employee e6=new Employee("S007","米莉","女",24,d2,p3); //调用addEmployee方法完成员工与部门的双向关联 d1.addEmployee(e1); d1.addEmployee(e2); d1.addEmployee(e3); d2.addEmployee(e4); d2.addEmployee(e5); d2.addEmployee(e6); System.out.println("员工信息如下:"); System.out.println(e1.introduction()); System.out.println("======================================="); System.out.println(e2.introduction()); System.out.println("======================================="); System.out.println(e3.introduction()); System.out.println("======================================="); System.out.println(e4.introduction()); System.out.println("======================================="); System.out.println(e5.introduction()); System.out.println("======================================="); System.out.println(e6.introduction()); System.out.println("======================================="); System.out.println("人事部总共有"+d1.getMyEmployeeNum()+"名员工!"); System.out.println("市场部总共有"+d2.getMyEmployeeNum()+"名员工!"); System.out.println("人事部总共有"+e2.getEmployeeDep().getMyEmployeeNum()+"名员工!"); System.out.println("市场部总共有"+e6.getEmployeeDep().getMyEmployeeNum1()+"名员工!"); }
但是实际上可以用部门来统计部门里的人数:
public static void main(String[] args) { //实例化部门 Department d1=new Department("部门01","人事部"); Department d2=new Department("部门02","市场部"); //实例化职务 Post p1=new Post("职务01","经理"); Post p2=new Post("职务02","助理"); Post p3=new Post("职务03","职员"); //实例化员工 Employee e1=new Employee("S001","张铭","hah",90,d1,p1); Employee e2=new Employee("S002","李艾爱","女",21,d1,p2); Employee e3=new Employee("S004","孙超","男",29,d1,p3); Employee e4=new Employee("S005","张美美","女",26,d2,p3); Employee e5=new Employee("S006","蓝迪","男",37,d2,p1); Employee e6=new Employee("S007","米莉","女",24,d2,p3); //调用addEmployee方法完成员工与部门的双向关联 d1.addEmployee(e1); d1.addEmployee(e2); d1.addEmployee(e3); d2.addEmployee(e4); d2.addEmployee(e5); d2.addEmployee(e6); System.out.println("员工信息如下:"); System.out.println(e1.introduction()); System.out.println("======================================="); System.out.println(e2.introduction()); System.out.println("======================================="); System.out.println(e3.introduction()); System.out.println("======================================="); System.out.println(e4.introduction()); System.out.println("======================================="); System.out.println(e5.introduction()); System.out.println("======================================="); System.out.println(e6.introduction()); System.out.println("======================================="); System.out.println("人事部总共有"+d1.getMyEmployeeNum()+"名员工!"); System.out.println("市场部总共有"+d2.getMyEmployeeNum()+"名员工!");
我觉d1.getMyEmployeeNum()与d2.getMyEmployeeNum()不好理解,我在代码中为什么只需要一个员工人数的属性即myEmployeeNum就可以统计,为什么部门间就能分别智能的统计,而不是一个3人一个6人得到的是累加的结果。希望老师解答一下,谢谢0.0!
41
收起
正在回答
1回答
if(this.getMyEmployee()[i].getEmployeeDep().getDepartmentNam().equals("人事部")) this.myEmployeeNum+=1;//统计人事部员工人数 if(this.getMyEmployee()[i].getEmployeeDep().getDepartmentNam().equals("市场部")) this.myEmployeeNum1+=1;//统计市场部员工人数
这段代码,虽然能解决程序最后要求的效果,但是,实际是不符合面向对象编程的特征的。试想一下,如果题目后续扩展要求,公司规模扩张,有100个部门,你的代码该如何设计呢?
解决思路:因为每次实例化一个部门类的对象,就会产生一个部门实例,所以,当完成名称属性设置,执行添加人员方法时,就是在指定部门加入职员了。
譬如:当设置部门名称为“人事部”,执行添加员工的方法时,就是在人事部中添加新员工了。
因此:部门类中添加方法中无需进行条件判断。
this.myEmployeeNum+=1;
直接使用这个即可实现相同的效果。
而这也就是,为什么你的第二段main方法中,直接使用d1,d2两个部门对象即可获取信息的套路啦~~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星