简单Java类深入
①实现基本字段的转换:
class Emp {
private String ename;
private int empno;
private double sal;
private double comm;
private String job;
//set.get略
public Emp() {}
public Emp(String ename,int empno,double sal,double comm,String job) {
this.ename=ename;
this.empno=empno;
this.sal=sal;
this.comm=comm;
this.job=job;
}
public String getInfo(){
return ename+empno+sal+comm+job;
}
}
class Dept {
private int deptno;
private String dname;
//set.get略
public Dept(){}
public Dept(int deptno,String dname){
this.deptno=deptno;
this.dname=dname;
}
public String getInfo(){
return deptno+dname;
}
}
public class person {
public static void main (String args[]){
}
}
②解决外键关系
1 class Emp { 2 private String ename; 3 private int empno; 4 private double sal; 5 private double comm; 6 private String job; //set.get略 7 private Dept dept; 8 private Emp mgr; 9 public void setDept(Dept dept){ 10 this.dept=dept; 11 } 12 public void setMgr(Emp mgr){ 13 this.mgr=mgr; 14 } 15 public Dept getDept(){ 16 return this.dept; 17 } 18 public Emp getMgr(){ 19 return this.mgr; 20 } 21 public Emp() {} 22 public Emp(String ename,int empno,double sal,double comm,String job) { 23 this.ename=ename; 24 this.empno=empno; 25 this.sal=sal; 26 this.comm=comm; 27 this.job=job; 28 } 29 public String getInfo(){ 30 return ename+empno+sal+comm+job;//this.略 31 } 32 } 33 class Dept { 34 private int deptno; 35 private String dname;//set.get略 36 private Emp emps [] ; 37 public void setEmps(Emp [] emps ){ 38 this.emps=emps; 39 } 40 public Emp [] getEmps(){ 41 return this.emps; 42 } 43 public Dept(){} 44 public Dept(int deptno,String dname){ 45 this.deptno=deptno; 46 this.dname=dname; 47 } 48 public String getInfo(){ 49 return deptno+dname;//this略 50 } 51 } 52 public class person { 53 public static void main (String args[]){ 54 55 } 56 }
③设置数据与取出数据
class Emp { private String ename; private int empno; private double sal; private double comm; private String job; //set.get略 private Dept dept; private Emp mgr; 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 Emp() {} public Emp(String ename,int empno,double sal,double comm,String job) { this.ename=ename; this.empno=empno; this.sal=sal; this.comm=comm; this.job=job; } public String getInfo(){ return "姓名: "+ename+", 编号: "+empno+", 工资: "+sal+", 佣金: "+comm+", 职位: "+job;//this.略 } } class Dept { private int deptno; private String dname;//set.get略 private Emp emps [] ; public void setEmps(Emp [] emps ){ this.emps=emps; } public Emp [] getEmps(){ return this.emps; } public Dept(){} public Dept(int deptno,String dname){ this.deptno=deptno; this.dname=dname; } public String getInfo(){ return "编号: "+deptno+", 客栈名称: "+dname;//this略 } } public class person { public static void main (String args[]){ Emp aa = new Emp("阿一",1,111.1,1.1,"炊事部"); Emp bb = new Emp("小二",2,222.2,2.2,"伙计"); Emp cc = new Emp("张三",3,333.3,3.3,"客栈老板"); Dept dd = new Dept(4,"安居客栈"); aa.setMgr(cc); bb.setMgr(cc); aa.setDept(dd); bb.setDept(dd); cc.setDept(dd); dd.setEmps(new Emp[] {aa,bb}); System.out.println(aa.getInfo()); System.out.println(aa.getMgr().getInfo()); System.out.println(aa.getDept().getInfo()); System.out.println("******************"); System.out.println("******************"); for (int x=0;x<dd.getEmps().length ;x++ ) { System.out.println(dd.getEmps()[x].getInfo() ); System.out.println(dd.getEmps()[x].getMgr().getInfo() ); } } }