JAVA | 5

来源:互联网 发布:大数据 云计算比较 编辑:程序博客网 时间:2024/06/05 08:48
package com.company;//定义职员类class Emp{    private int empno;    private String ename;    private String job;    private double sal;    //定义部门信息    private Dept dept;    public void setDept(Dept dept){        this.dept = dept;    }    public Dept getDept(){        return this.dept;    }    //定义领导信息    private Emp mgr;    public void setMgr(Emp mgr){        this.mgr = mgr;    }    public Emp getMgr(){        return this.mgr;    }    //setter,getter,无参构造略    //构造函数    public Emp(int empno, String ename, String job, double sal){        this.empno = empno;        this.ename = ename;        this.job = job;        this.sal = sal;    }    //返回职员信息    public String getInfo(){        return "职员编号: " + this.empno + "\n" +                "职员姓名: " + this.ename + "\n" +                "职位: " + this.job + "\n" +                "工资: " + this.sal + "\n";    }}//定义部门类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;    }    //setter,getter,无参构造略    //构造函数    public Dept(int deptno, String dname, String loc){        this.deptno = deptno;        this.dname = dname;        this.loc = loc;    }    //返回部门信息    public String getInfo(){        return "部门编号: " + this.deptno + "\n" +                "部门名称: " + this.dname + "\n" +                "部门地址: " + this.loc + "\n";    }}//主函数public class Main {    public static void main(String[] args) {        //实例化职员和部门信息        Dept dept = new Dept(4396,"Accounting","Beijing");        Emp empA = new Emp(1,"Jack","Clerk",1000);        Emp empB = new Emp(2,"Tom","Manager",2000);        Emp empC = new Emp(3,"Marry","President",3000);        //设置职员和领导关系        empA.setMgr(empB);        empB.setMgr(empC);        //设置职员和部门关系        empA.setDept(dept);        empB.setDept(dept);        empC.setDept(dept);        dept.setEmps(new Emp[]{empA,empB,empC});        //根据职员找到领导信息和部门信息        System.out.println(empA.getInfo() + "\n" + empA.getMgr().getInfo() + "\n" + empA.getDept().getInfo());        //根据部门找到职员信息        System.out.println(dept.getInfo());        for(int i = 0; i < dept.getEmps().length; i ++){            System.out.println(dept.getEmps()[i].getInfo() + dept.getEmps()[i].getDept().getInfo());            if(dept.getEmps()[i].getMgr() != null) {                System.out.println(dept.getEmps()[i].getMgr().getInfo());            }        }    }}
原创粉丝点击