为了账号安全,请及时绑定邮箱和手机立即绑定

Java简单表的映射练习

标签:
Java
/*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());
          }
       }

    }
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
18

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消