开发者社区 问答 正文

使用类集实现以下数据表和简单Java类的映射实现;

使用类集实现以下数据表和简单Java类的映射实现;

展开
收起
游客pklijor6gytpx 2019-11-21 15:03:57 559 分享 版权
1 条回答
写回答
取消 提交回答
  • importjava.util.Iterator;
    importjava.util.List;
    classEmp{//emp表映射类
    privateintempno;
    privateStringename;
    privateStringjob;
    privatedoublesal;
    privatedoublecomm;
    privateEmpmgr;//雇员领导
    privateDeptdept;//雇员所属部门
    publicEmp(){//无参构造
    }
    publicEmp(intempno,Stringename,Stringjob,doublesal,doublecomm){
    this.empno=empno;
    this.ename=ename;
    this.job=job;
    this.sal=sal;
    this.comm=comm;
    }
    //部分setter、getter略
    publicvoidsetDept(Deptdept){//设置雇员所在部门
    this.dept=dept;
    }
    publicDeptgetDept(){//取得雇员所在部门
    returnthis.dept;
    }
    publicvoidsetMgr(Empmgr){//设置雇员领导
    this.mgr=mgr;
    }
    publicEmpgetMgr(){//取得雇员领导
    returnthis.mgr;
    }
    @Override
    publicinthashCode(){
    finalintprime=31;
    intresult=1;
    longtemp;
    temp=Double.doubleToLongBits(comm);
    result=prime*result+(int)(temp^(temp>>>32));
    result=prime*result+((dept==null)?0:dept.hashCode());
    result=prime*result+empno;
    result=prime*result+((ename==null)?0:ename.hashCode());
    result=prime*result+((job==null)?0:job.hashCode());
    result=prime*result+((mgr==null)?0:mgr.hashCode());
    temp=Double.doubleToLongBits(sal);
    result=prime*result+(int)(temp^(temp>>>32));
    returnresult;
    }
    @Override
    publicbooleanequals(Objectobj){
    if(this==obj)
    returntrue;
    if(obj==null)
    returnfalse;
    if(getClass()!=obj.getClass())
    returnfalse;
    Empother=(Emp)obj;
    if(Double.doubleToLongBits(comm)!=Double
    .doubleToLongBits(other.comm))
    returnfalse;
    if(dept==null){
    if(other.dept!=null)
    returnfalse;
    }elseif(!dept.equals(other.dept))
    returnfalse;
    if(empno!=other.empno)
    returnfalse;
    if(ename==null){
    if(other.ename!=null)
    returnfalse;
    }elseif(!ename.equals(other.ename))
    returnfalse;
    if(job==null){
    if(other.job!=null)
    returnfalse;
    }elseif(!job.equals(other.job))
    returnfalse;
    if(mgr==null){
    if(other.mgr!=null)
    returnfalse;
    }elseif(!mgr.equals(other.mgr))
    returnfalse;
    if(Double.doubleToLongBits(sal)!=Double.doubleToLongBits(other.sal))
    returnfalse;
    returntrue;
    }
    @Override
    publicStringtoString(){//取得雇员信息
    return"雇员编号:"+this.empno+",姓名:"+this.ename+",职位:"+this.job
    +",工资:"+this.sal+",佣金:"+this.comm;
    }
    }
    classDept{//dept表映射类
    privateintdeptno;
    privateStringdname;
    privateStringloc;
    privateList<Emp>emps;//多个雇员
    publicDept(){//无参构造
    this.emps=newArrayList<Emp>();
    }
    publicDept(intdeptno,Stringdname,Stringloc){
    this();//调用无参构造
    this.deptno=deptno;
    this.dname=dname;
    this.loc=loc;
    }
    //部分setter、getter略
    publicvoidsetEmps(List<Emp>emps){
    this.emps=emps;
    }
    publicList<Emp>getEmps(){
    returnemps;
    }
    @Override
    publicStringtoString(){//取得部门信息
    return"部门编号:"+this.deptno+",部门名称:"+this.dname+",位置:"
    +this.loc;
    }
    }
    publicclassTestDemo{
    publicstaticvoidmain(Stringargs[]){
    //1、第一层配置关系
    Deptdept=newDept(10,"ACCOUNTING","NewYrok");
    Empempa=newEmp(7369,"SMITH","CLERK",800.0,0.0);
    Empempb=newEmp(7566,"ALLEN","MANAGER",2450.0,0.0);
    Empempc=newEmp(7839,"KING","PRESIDENT",5000.0,0.0);
    empa.setMgr(empb);//设置雇员和领导的关系
    empb.setMgr(empc);//设置雇员和领导的关系
    empa.setDept(dept);//每个雇员属于一个部门
    empb.setDept(dept);//每个雇员属于一个部门
    empc.setDept(dept);//每个雇员属于一个部门
    //每一个部门有多个雇员,通过对象数组表示多个雇员
    dept.getEmps().add(empa);
    dept.getEmps().add(empb);
    dept.getEmps().add(empc);
    //2、第二层取得关系
    System.out.println(dept);
    Iterator<Emp>iter=dept.getEmps().iterator();
    while(iter.hasNext()){
    Empemp=iter.next();
    System.out.println(emp);
    if(emp.getMgr()!=null){//有领导
    System.out.println("\t"+emp.getMgr());
    }
    System.out.println("---------------------------------------------------");
    }
    }
    }
    
    2019-11-21 15:04:49
    赞同 展开评论