Java简单类(部门、领导、雇员关系)

 1 class Dept {
 2     private int deptno ;
 3     private String dname ;
 4     private String loc ;
 5     private Emp emps [] ;    //多个雇员
 6     public void setEmps(Emp [] emps) {
 7         this.emps = emps ;
 8     }
 9     public Emp[] getEmps() {
10         return this.emps ;
11     }
12     public Dept(int deptno,String dname,String loc){
13         this.deptno = deptno ;
14         this.dname = dname ;
15         this.loc = loc ;    
16     }
17     public String getInfo() {
18         return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc ;
19     }
20 }
21 class Emp {
22     private int empno ;
23     private String ename ;
24     private String job ;
25     private double sal ;
26     private double comm ;
27     private Dept dept ;//表示对应的部门信息
28     private Emp mgr ;  //表示雇员对应的领导
29     public void setMgr(Emp mgr) {
30         this.mgr = mgr ;
31     }
32     public Emp getMgr() {
33         return this.mgr ;
34     }
35     public void setDept(Dept dept){
36         this.dept = dept ;
37     }
38     public Dept getDept() {
39         return this.dept ;
40     }
41     public Emp(int empno,String ename,String job,double sal,double comm){
42         this.empno = empno ;
43         this.ename = ename ;
44         this.job = job ;
45         this.sal = sal ;
46         this.comm = comm ; 
47     }
48     public String getInfo(){
49         return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ;
50     }
51 }
52 public class  Test2 {
53     public static void main(String args[]) {
54         Dept dept = new Dept(10,"ACCOUNTING","CHINA") ;  //部门信息
55         Emp ea = new Emp(7369,"CHEN","CLERK",800.0,0.0);//雇员信息
56         Emp eb = new Emp(7902,"PENG","MANAGER",2500.0,0.0);//雇员信息
57         Emp ec = new Emp(7719,"KING","PRESIDENT",5000.0,0.0);//雇员信息        
58         ea.setMgr(eb) ; //雇员与领导
59         eb.setMgr(ec) ; //雇员与领导
60         ea.setDept(dept) ; //雇员与部门
61         eb.setDept(dept) ; //雇员与部门
62         ec.setDept(dept) ; //雇员与部门
63         dept.setEmps(new Emp[]{ea,eb,ec}) ;
64         System.out.println(ea.getInfo()) ;//通过雇员找到领导信息和部门信息
65         System.out.println("\t|-"+ ea.getMgr().getInfo()) ;
66         System.out.println("\t|-"+ ea.getDept().getInfo()) ;
67         System.out.println("----------------------------------------") ;
68         System.out.println(dept.getInfo()) ;//根据部门找到所有的雇员以及每个雇员的领导信息
69         for (int x = 0 ;x < dept.getEmps().length ;x ++ ){
70             System.out.println("\t|-"+ dept.getEmps()[x].getInfo()) ;
71             if (dept.getEmps()[x].getMgr() != null){
72                 System.out.println("\t|-"+dept.getEmps()[x].getMgr().getInfo()) ;
73             }
74         }
75     }
76 }

一个部门中有多个雇员,每个雇员有一个领导或者没有领导

posted @ 2016-03-07 00:31  蒋立  阅读(936)  评论(1编辑  收藏  举报