采菊大侠
2016-12-13 22:49:59浏览 3355
/*EMP-Dept 简单表的映射
*简单一对一
*/
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 deptno;
private Emp mgr;
public void setDeptno(Dept deptno){
this.deptno = deptno;
}
public Dept getDeptno(){
return this.deptno;
}
public void setMgr(Emp mgr){
this.mgr = mgr;
}
public Emp getMgr(){
return this.mgr = mgr;
}
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 getMgrInfo(){//扩充一个取得领导信息的方法
return "领导编号:"+this.empno+",雇员名字:"+this.ename+",职位:"+this.job+",工资:"+this.sal+",佣金:"+this.comm;
}
public String getInfo(){
return "雇员编号:"+this.empno+",雇员名字:"+this.ename+",职位:"+this.job+",工资:"+this.sal+",佣金:"+this.comm;
}
}入代码
public class DeptEmpTest{
public static void main(String[] args){
Dept dept = new Dept(18,"ACCOUNTANT","CHIAN");
Emp ea = new Emp(1,"SMITH","CHERK",250,0.0);
Emp eb = new Emp(2,"FROD","MANAGER",250,0.0);
Emp ec = new Emp(3,"KING","PRESIDENT",250,0.0);
//设置雇员-领导关系
ea.setMgr(eb);
eb.setMgr(ec);
//设置雇员-部门关系
ea.setDeptno(dept);
eb.setDeptno(dept);
ec.setDeptno(dept);
//设置部门-雇员关系
dept.setEmps(new Emp[] {ea,eb,ec});
System.out.println(ea.getInfo());
System.out.println("\t|-"+ea.getMgr().getMgrInfo());
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
for(int x=0; x<dept.getEmps().length;x++){
System.out.println(dept.getEmps()[x].getInfo());
if(dept.getEmps()[x].getMgr() != null){
System.out.println("\t|-"+dept.getEmps()[x].getMgr().getMgrInfo());
}
}
}
}