人事部只有3名员工,统计结果为6名

人事部只有3名员工,统计结果为6名

package cn.imooc;
//测试类
public class Test {
	public static void main(String[] args) {
		/*
		 * 实例化职务
		 */
		Position position=new Position(001, "经理");
		Position position2=new Position(002, "助理");
		Position position3=new Position(003, "职员");
		/*
		 * 实例化部门
		 */
		Department department=new Department(002, "人事部");
		Department department2=new Department(002, "市场部");
		/*
		 * 实例化员工类
		 */
		Employee employee=new Employee("张铭", "S001", 29, "男", department, position);
		Employee employee2=new Employee("李艾爱", "S002", 21, "女", department, position2);
		Employee employee3=new Employee("孙超", "S003", 29, "男", department, position3);
		Employee employee4=new Employee("张美美", "S004", 26, "女", department2, position3);
		Employee employee5=new Employee("蓝迪", "S006", 37, "男", department2, position);
		Employee employee6=new Employee("米莉", "S007", 24, "男", department2, position3);
		/*
		 * 输出员工基本信息
		 */
		System.out.println(employee.show()+"\n"+"=============");
		System.out.println(employee2.show()+"\n"+"=============");
		System.out.println(employee3.show()+"\n"+"=============");
		System.out.println(employee4.show()+"\n"+"=============");
		System.out.println(employee5.show()+"\n"+"=============");
		System.out.println(employee6.show()+"\n"+"=============");
		/*
		 * 调用部门人员统计方法
		 */
		department.addEmployee(employee);
		department.addEmployee(employee2);
		department.addEmployee(employee3);
		department.addEmployee(employee4);
		department.addEmployee(employee5);
		department.addEmployee(employee6);
		department2.addEmployee(employee6);
		/*
		 * 输出部门统计结果
		 */
		System.out.println(department.getEmployeeNum());
		System.out.println(department2.getEmployeeNum());

	}

}



package cn.imooc;

//部门类

public class Department {

private int departmentId;//部门ID

private String departmentName;//部门名称

private Employee[] employees;//部门员工信息存储数组

private int employeeNum;//部门员工信息统计


public Department() {


}

public Department(int departmentId, String departmentName) {

super();

setDepartmentId(departmentId);

setDepartmentName(departmentName);

}

public Department(int departmentId, String departmentName, Employee[] employees, int employeeNum) {

super();

setDepartmentId(departmentId);

setDepartmentName(departmentName);

setEmployees(employees);

setEmployeeNum(employeeNum);

}

public int getDepartmentId() {

return departmentId;

}

public void setDepartmentId(int departmentId) {

this.departmentId = departmentId;

}

public String getDepartmentName() {

return departmentName;

}

public void setDepartmentName(String departmentName) {

this.departmentName = departmentName;

}


public Employee[] getEmployees() {

if (this.employees==null) {

employees=new Employee[200];

}

return employees;

}


public void setEmployees(Employee[] employees) {

this.employees = employees;

}


public int getEmployeeNum() {


return employeeNum;

}


public void setEmployeeNum(int employeeNum) {

this.employeeNum = employeeNum;

}

/**

* 部门员工统计方法

* @param employee

*/

public void addEmployee(Employee employee) {

for (int i = 0; i < getEmployees().length; i++) {

if(this.getEmployees()[i]==null) {

this.getEmployees()[i]=employee;

this.employeeNum=i+1;

return;

}

}

}


}

package cn.imooc;

//职务类

public class Position {

private int positionId;//职务ID

private String positionName;//职务名称

public Position() {

}

public Position(int positionId, String positionName) {

super();

setPositionId(positionId);

setPositionName(positionName);

}

public int getPositionId() {

return positionId;

}

public void setPositionId(int positionId) {

this.positionId = positionId;

}

public String getPositionName() {

return positionName;

}

public void setPositionName(String positionName) {

this.positionName = positionName;

}

}

package cn.imooc;

//员工类

public class Employee {

private String name;//姓名

private String id;//工号

private int age;//年龄

private String gender;//性别

private Department department;//部门

private Position position;//职务

public Employee() {


}

public Employee(String name, String id, int age, String gender, Department department, Position position) {

super();

setName(name);

setAge(age);

setId(id);

setGender(gender);

setDepartment(department);

setPosition(position);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name=name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public Department getDepartment() {

return department;

}

public void setDepartment(Department department) {

this.department = department;

}

public Position getPosition() {

return position;

}

public void setPosition(Position position) {

this.position = position;

}

/**

* 员工信息展示方法

* @return返回员工基本信息

*/

public String show() {

return "姓名:"+getName()+"\r"+"工号:"+getId()+"\r"+"性别:"+getGender()+"\r"+"年龄:"+getAge()+

"\r"+"职务:"+department.getDepartmentName()+position.getPositionName();

}

}


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

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

1回答
好帮手慕阿满 2018-10-29 11:39:06

同学的测试类中调用人员统计方法时,使用的department.addEmployee(employee);   department表示的是人事部,将所有的人员都添加进来的人事部,之后用使用department2.addEmployee(employee6);添加进了市场部,所以人事部6人,市场部1人。正确应该是三个添加进人事部,三个添加进市场部,代码如图:

http://img1.sycdn.imooc.com//climg/5bd6808e00010d3f04850212.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 qq_耿乾_0 #1
    为什么不会根据调用的对象区分开市场部和人事部呢
    2018-10-29 11:50:36
  • 好帮手慕阿满 回复 提问者 qq_耿乾_0 #2
    还有一处需要修改的,建议在定义人事部和市场部时,将其部门编号要定义为不同的。 这里的department明确表示了这是人事部,使用department调用addEmployee()使得添加的员工都是人事部的。这里使用了双向关联,使得员工和部门相互关联,员工属于某个部门,部门中添加了该员工。祝:学习愉快~
    2018-10-29 14:25:28
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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