public class Emp { //雇员类
private int id; //雇员编号
private String name; //雇员姓名
private String job; //雇员职位
private double sal; //雇员薪水
private Emp mgr; //雇员领导
private Dept dept; //雇员部门
public Emp(int id,String name,String job,double sal) { //构造方法
this.id = id;
this.name = name;
this.job = job;
this.sal = sal;
}
public int getId() { //获取雇员id
return this.id;
}
public String getName() { //获取雇员姓名
return this.name;
}
public String getJob() { //获取雇员职位
return this.job;
}
public double getSal() { //获取雇员薪水
return this.sal;
}
public void setMgr(Emp mgr) { //设置雇员领导
this.mgr = mgr;
}
public Emp getMgr() { //获取雇员领导
return this.mgr;
}
public String getEmpInfo() { //获取雇员全部信息
return "雇员编号:"+this.getId()+
"\n雇员姓名:"+this.getName()+
"\n雇员职位:"+this.getJob()+
"\n雇员薪水:"+this.getSal();
}
public void setDept(Dept dept) { //设置雇员所在部门
this.dept = dept;
}
public Dept getDept() { //获取雇员的部门信息
return this.dept;
}
public static void main(String[] args) {
Dept dept = new Dept(10,"技术部");
Emp empa = new Emp(7369,"张三","前端工程师",8000.0);
Emp empb = new Emp(7566,"李四","web开发工程师",6000);
Emp empc = new Emp(7839,"王五","java工程师",9000.0);
empa.setMgr(empb); //设置雇员和领导的关系
empb.setMgr(empc); //设置雇员和领导的关系
empc.setMgr(null);
empa.setDept(dept); //每个雇员属于一个部门
empb.setDept(dept); //每个雇员属于一个部门
empc.setDept(dept); //每个雇员属于一个部门
dept.setEmps(new Emp[] {empa,empb,empc});//每个部门有多个雇员
System.out.print("\t"+dept.getDeptInfo());//输出部门信息
System.out.println("\n--------------------------------------------------------");
for(int i=0;i<dept.getEmps().length;i++)
{
System.out.println(dept.getEmps()[i].getEmpInfo());
System.out.println("\n--------------------------------------------------------");
}
}
}
class Dept{ //部门类
private int deptno; //部门编号
private String name; //部门名称
private Emp emps[]; //一个部门有多个雇员
public Dept(int no,String name) { //构造方法
this.deptno = no;
this.name = name;
}
public int getDeptno() { //获取部门编号
return this.deptno;
}
public String getDeptname() { //获取部门名称
return this.name;
}
public void setEmps(Emp emps[]) { //设置雇员的全部雇员
this.emps = emps;
}
public Emp[] getEmps() { //返回部门的全部雇员
return this.emps;
}
public String getDeptInfo() { //获取部门全部信息
return "部门编号:"+this.getDeptno()+",部门名称:"+this.getDeptname()+"。";
}
}
输出结果:
部门编号:10,部门名称:技术部。
雇员编号:7369
雇员姓名:张三
雇员职位:前端工程师
雇员薪水:8000.0
雇员编号:7566
雇员姓名:李四
雇员职位:web开发工程师
雇员薪水:6000.0
雇员编号:7839
雇员姓名:王五
雇员职位:java工程师
雇员薪水:9000.0