zoukankan      html  css  js  c++  java
  • 数据表与简单java类(一对多的关系)

     

     

     1 public class Newbegin{
     2 public static void main(String args[]) {
     3  //第一步:设置类对象间的关系
     4  //1.分别创建各自类的实例化对象,
     5  Dept dept=new Dept(10,"ACCOUNTING","NEW YORK");
     6  Emp ea=new Emp(7369,"SMITH","CLERY",800.0,0.0);
     7  Emp eb=new Emp(7566,"ALLEN","MANAGER",2450.0,0.0);
     8  Emp ec=new Emp(7839,"KING","PRESIDENT",5000.0,0.0);
     9  //2.设置雇员领导的关系! 
    10  ea.setMgr(eb);
    11  eb.setMgr(ec);//ec对象没有领导,因为他是头
    12  //3.设置雇员和部门的关系
    13  ea.setDept(dept);
    14  eb.setDept(dept);
    15  eb.setDept(dept);
    16  //4.设置部门和雇员的关系
    17  dept.setEmps(new Emp[] {ea,eb,ec});
    18  //第二步:数据的取得
    19  //5.一个部门有多个雇员,并且可以输出一个部门的完整信息(包括雇员信息)
    20  System.out.println(dept.getInfo());//输出部分信息 
    21  //部门中的所有雇员
    22  //雇员的领导
    23  for(int x=0;x<dept.getEmps().length;x++){
    24   System.out.println("	-"+dept.getEmps()[x].getInfo());
    25   if (dept.getEmps()[x].getMgr()!=null)
    26   {
    27    System.out.println("		|-"+dept.getEmps()[x].getMgr().getInfo());
    28   }
    29  }
    30  System.out.println("-----------");
    31  //可以根据一个雇员找到对应的领导信息和所在部门的信息
    32  System.out.println(eb.getInfo());
    33  if(eb.getMgr()!=null){
    34   System.out.println("	|-"+eb.getMgr().getInfo());
    35  }
    36  if(eb.getDept()!=null){
    37   System.out.println(eb.getDept().getInfo());
    38  }
    39 }
    40 }
    41 class Emp
    42 {
    43  private int empno;
    44  private String ename;
    45  private String job;
    46  private double sal;
    47  private double comm;
    48  private Emp mgr;//描述雇员领导!
    49  private Dept dept;//描述雇员所在的部门! 
    50  public Emp(){}
    51  public Emp(int empno,String ename,String job,double sal,double comm){
    52   this.empno=empno;
    53   this.ename=ename;
    54   this.job=job;
    55   this.sal=sal;
    56   this.comm=comm;
    57  }
    58  public void setMgr(Emp mgr){
    59   this.mgr=mgr;
    60  }
    61  public Emp getMgr(){
    62   return this.mgr;
    63  }
    64  public void setDept(Dept dept){
    65   this.dept=dept;
    66  } 
    67  public Dept getDept(){
    68   return this.dept;
    69  }
    70  public String getInfo(){
    71   return "【EMP】empno+"+this.empno+
    72    ",ename=" +this.ename+
    73    ",job="+this.job+
    74    ",sal="+this.sal+
    75    ",comm="+this.comm;
    76  }
    77 }
    78 class Dept
    79 {
    80  private int deptno;
    81  private String dname;
    82  private String loc;
    83  private Emp[] emps;
    84  public Dept(){}
    85  public Dept(int deptno,String dname,String loc){
    86   this.deptno=deptno;
    87   this.dname=dname;
    88   this.loc=loc;
    89  }
    90  public void setEmps(Emp[] emps){
    91   this.emps=emps;
    92  }
    93  public Emp[] getEmps(){
    94   return this.emps;
    95  }
    96  public String getInfo(){
    97   return "【DEPT】deptno="+deptno+",dnema="+dname+",loc="+loc;
    98  }
    99 }

     

     

     

  • 相关阅读:
    tensorflow2.0 GPU和CPU 时间对比
    第一次使用FileZilla Server
    PremiumSoft Navicat 15 for Oracle中文破解版安装教程
    Unmapped Spring configuration files found. Please configure Spring facet or use 'Create Default Context' to add one including all unmapped files.
    ng : 无法加载文件 D: odejs ode_global g.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
    angular
    Github上优秀的go项目
    win10---file explore 中remove quick access folder
    react--useEffect使用
    linux---cat 和 grep 的妙用
  • 原文地址:https://www.cnblogs.com/Tony98/p/10387391.html
Copyright © 2011-2022 走看看