简单java类和数据表的映射

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值