当前位置:   article > 正文

java 从入门到项目实战 例程_java入门项目实例

java入门项目实例

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( "-------------------------------" );
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/556253
推荐阅读
相关标签
  

闽ICP备14008679号