Java简单类(部门、领导、雇员关系)

 class Dept {
private int deptno ;
private String dname ;
private String loc ;
private Emp emps [] ; //多个雇员
public void setEmps(Emp [] emps) {
this.emps = emps ;
}
public Emp[] getEmps() {
return this.emps ;
}
public Dept(int deptno,String dname,String loc){
this.deptno = deptno ;
this.dname = dname ;
this.loc = loc ;
}
public String getInfo() {
return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc ;
}
}
class Emp {
private int empno ;
private String ename ;
private String job ;
private double sal ;
private double comm ;
private Dept dept ;//表示对应的部门信息
private Emp mgr ; //表示雇员对应的领导
public void setMgr(Emp mgr) {
this.mgr = mgr ;
}
public Emp getMgr() {
return this.mgr ;
}
public void setDept(Dept dept){
this.dept = dept ;
}
public Dept getDept() {
return this.dept ;
}
public Emp(int empno,String ename,String job,double sal,double comm){
this.empno = empno ;
this.ename = ename ;
this.job = job ;
this.sal = sal ;
this.comm = comm ;
}
public String getInfo(){
return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ;
}
}
public class Test2 {
public static void main(String args[]) {
Dept dept = new Dept(10,"ACCOUNTING","CHINA") ; //部门信息
Emp ea = new Emp(7369,"CHEN","CLERK",800.0,0.0);//雇员信息
Emp eb = new Emp(7902,"PENG","MANAGER",2500.0,0.0);//雇员信息
Emp ec = new Emp(7719,"KING","PRESIDENT",5000.0,0.0);//雇员信息
ea.setMgr(eb) ; //雇员与领导
eb.setMgr(ec) ; //雇员与领导
ea.setDept(dept) ; //雇员与部门
eb.setDept(dept) ; //雇员与部门
ec.setDept(dept) ; //雇员与部门
dept.setEmps(new Emp[]{ea,eb,ec}) ;
System.out.println(ea.getInfo()) ;//通过雇员找到领导信息和部门信息
System.out.println("\t|-"+ ea.getMgr().getInfo()) ;
System.out.println("\t|-"+ ea.getDept().getInfo()) ;
System.out.println("----------------------------------------") ;
System.out.println(dept.getInfo()) ;//根据部门找到所有的雇员以及每个雇员的领导信息
for (int x = 0 ;x < dept.getEmps().length ;x ++ ){
System.out.println("\t|-"+ dept.getEmps()[x].getInfo()) ;
if (dept.getEmps()[x].getMgr() != null){
System.out.println("\t|-"+dept.getEmps()[x].getMgr().getInfo()) ;
}
}
}
}

一个部门中有多个雇员,每个雇员有一个领导或者没有领导

Java简单类(部门、领导、雇员关系)

上一篇:STL——容器(Map & multimap)的大小


下一篇:CentOS下利用Docker部署Surging