赞
踩
P127
package JavaBookstudy; class Dept { private long deptno ; private String dname ; private String loc ; private Emp emps[] ; public Dept (long deptno , String dname , String loc ) { this.deptno = deptno ; this.dname = dname ; this. loc = loc ; } public void setEmps (Emp emps [] ) { this.emps = emps ; } public Emp[] getEmps () { return this.emps ; } public String getInfo() { return "【部门信息】 部门编号 = " + this.deptno + "、部门名称 ="+ this.dname + "、部门位置 = " + this.loc ; } } class Emp { private long empno ; private String ename ; private String job ; private double sal ; private double comn ; private Dept dept ; private Emp mgr ; public Emp ( long empno , String ename , String job , double sal , double comn ) { this.empno = empno ; this.ename = ename ; this.job = job ; this.sal = sal ; this.comn = comn ; } public String getInfo () { return "【雇员信息】 编号 = " + this.empno + "、姓名 = " + this.ename + "、职位 = " + this.job + "、工资 = " + this.sal + "、佣金 = " + this.comn ; } public void setDept (Dept dept) { this.dept = dept ; } public void setMgr (Emp mgr) { this.mgr = mgr ; } public Dept getDept () { return this.dept ; } public Emp getMgr () { return this.mgr ; } } public class javaDemo { public static void main(String args[]){ Dept dept = new Dept (10 , "MLDN教学部" , "北京" ) ; Emp empA = new Emp ( 7369L , "SMITH" , "CLERK" , 800.00 , 0.0 ) ; Emp empB = new Emp ( 7566L , "FORD" , "MANAGER" , 2450.00 , 0.0 ) ; Emp empC = new Emp ( 7839L , "KING" , "PRESIDENT" , 5000.00 , 0.0 ) ; empA.setDept(dept) ; empB.setDept(dept) ; empC.setDept(dept) ; empA.setMgr(empB) ; empB.setMgr(empC) ; dept.setEmps( new Emp [] { empA , empB , empC } ) ; System.out.println(dept.getInfo()); for ( int x = 0 ; x < dept.getEmps().length ; x++ ) { System.out.println( "\t|- " +dept.getEmps()[x].getInfo() ); if (dept.getEmps()[x].getMgr() != null ) { System.out.println( "\t\t|- " + dept.getEmps()[x].getMgr().getInfo() ); } } System.out.println( "-------------------------------" );
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。