public class Employee {//员工类
private String name;
private int number;
private double salary;
private double bonus;
private Employee leader;
private Department department;

public Employee() {

}

public Employee(String name, int num, double salary, double bonus) {
this.name = name;
this.number = num;
this.salary = salary;
this.bonus = bonus;
}

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

public void setNumber(int number) {
this.number = number;
}

public void setSalary(double salary) {
this.salary = salary;
}

public void setBonus(double bonus) {
this.bonus = bonus;
}

public void setLeader(Employee lead) {
this.leader = lead;
}

public Employee getLeader() {
return leader;
}

public void setDepartment(Department department) {
this.department = department;
}

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

public int getNumber() {
return number;
}

public double getSalary() {
return salary;
}

public double getBonus() {
return bonus;
}

public Department getDepartment() {
return department;
}

public String getString() {
return "姓名:" + name + ",员工编号:" + number + ",工资:" + salary + ",年终奖:"
+ bonus + "--所属部门--" + department.getString();
}
}

//部门类

public class Department {
private String name;
private String loc;
private Employee[] emps = new Employee[] {};

public void setEmp(Employee[] emps) {
this.emps = emps;
}

public Employee[] getEmp() {
return this.emps;
}

public Department() {
// TODO Auto-generated constructor stub
}

public Department(String name, String loc) {
this.name = name;
this.loc = loc;
}

public String getString() {
return ":" + name + ",部门地址:" + loc;
}
}

//测试 客户端还可简写

public class Depat_Emp {
public static void main(String[] args) {
Department dt = new Department("销售部", "杭州");
Employee ea = new Employee("张三", 01, 500, 700);
Employee eb = new Employee("李四", 02, 800, 650);
Employee ec = new Employee("网六", 05, 900, 790);
ea.setDepartment(new Department());
// 设置员工所属部门
eb.setDepartment(dt);
ec.setDepartment(dt);
ea.setDepartment(dt);
// 员工所属领导
ea.setLeader(eb);
eb.setLeader(ec);
System.out.println("虽然员工对象设置了所属部门,但部门对象未设置员工对象之前它的长度为:"
+ dt.getEmp().length);
// 部门添加员工
dt.setEmp(new Employee[] { ea, eb, ec });
System.out.println(dt.getEmp()[0].getString());
System.out.println("部门对象设置了员工对象ea,eb,ec之后它的长度为:" + dt.getEmp().length);
System.out.println("员工对象ea的领导信息:\n" + ea.getLeader().getString());
System.out.println("----------------------------------");
// 部门的所有员工信息
for (Employee emp : dt.getEmp()) {// 把部门所有的员工遍历 类型为员工类
System.out.println(emp.getString());
if (emp.getLeader() != null) {
System.out.println("他的领导:" + emp.getLeader().getString());
System.out.println("====");
}
}
}
}