要进行关系设计,该数据表对应有如下的几个关系 一。 一个雇员属于一个部门,需要追加部门引用; 二。一个雇员有一个领导,领导一定是自身关联,自身引用 三。一个部门有多个雇员
//部门类class dept{ private int deptno;//部门编号 private string dname;//部门名称 private string loc;//部门位置 private emp emps[];//“引用关系” 多个雇员,描述多这个概念应该使用对象数组进行完成 //明确定义一个无参构造 public dept(){ } public dept(int deptno,string dname,string loc){//有参构造 this.deptno = deptno; this.dname = dname; this.loc = loc; } //setter方法 public void setdeptno(int d){ this.deptno = d; } public void setdname(string n){ this.dname = n; } public void setloc(string l){ this.loc = l; } //对象数组的setter方法 public void setemp(emp[] e){ this.emps = e ; } //getter方法 public int getdeptno(){ return deptno; } public string getdname(){ return dname; } public string getloc(){ return loc; } //对象数组的getter方法 public emp[] getemp(){ return emps; } //取得完整信息 public string getinfo(){ return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc; }}//员工类class emp{ private int empno; private string ename ; private string job ; private double sal; private double comm ; private dept dept;//“引用关系” 在雇员里面保存部门信息 private emp mgr;//“引用关系” 在雇员里面设置领导信息 public emp(){//无参构造 } public emp(int empno,string ename, string job, double sal, double comm){//有参构造 this.empno = empno; this.ename = ename ; this.job = job; this.sal = sal ; this.comm = comm ; } //setter方法 public void setempno(int e){ this.empno = e; } public void setename(string n){ this.ename = n ; } public void setjob(string j){ this.job = j ; } public void setsal(double s){ this.sal = s ; } public void setcomm(double c){ this.comm = c ; } public void setmgr(emp m){ this.mgr = m; } public void setdept(dept d){ this.dept = d; } //getter方法 public int getempno(){ return empno; } public string getename(){ return ename; } public string getjbo(){ return job; } public double getsal(){ return sal; } public double getcomm(){ return comm; } public emp getmgr(){ return mgr; } public dept getdept(){ return dept; } public string getinfo(){//取得完整信息 return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ; }}//测试类,设置数据并取出public class testdemo { public static void main(string[] args) { // 产生各自的独立对象,设置数据 dept dept = new dept(10,"accounting","new york"); emp ea = new emp(1234,"yiyao","clerk",900.0,0.0); emp eb = new emp(1235,"acan", "manager",4000.0,9.0); emp ec = new emp(1236,"dramatic","president",9000.0,80.0); //设置雇员和领导关系 ea.setmgr(eb); eb.setmgr(ec); //设置雇员和部门关系 ea.setdept(dept); eb.setdept(dept); ec.setdept(dept); //部门与雇员 dept.setemp(new emp[]{ea,eb,ec}); //取得数据 system.out.println(ea.getinfo()); system.out.println("\t|-" + ea.getmgr().getinfo()); system.out.println("\t|-" + ea.getdept().getinfo()); system.out.println("-----------------------------------------");//分割线 system.out.println(dept.getinfo()); for(int x = 0; x < dept.getemp().length; x++){ system.out.println("\t|-" + dept.getemp()[x].getinfo()); if(dept.getemp()[x].getmgr() != null){ system.out.println("\t\t|-" + dept.getemp()[x].getmgr().getinfo()); } } }}
//部门类class dept{ private int deptno;//部门编号 private string dname;//部门名称 private string loc;//部门位置 private emp emps[];//“引用关系” 多个雇员,描述多这个概念应该使用对象数组进行完成 //明确定义一个无参构造 public dept(){ } public dept(int deptno,string dname,string loc){//有参构造 this.deptno = deptno; this.dname = dname; this.loc = loc; } //setter方法 public void setdeptno(int d){ this.deptno = d; } public void setdname(string n){ this.dname = n; } public void setloc(string l){ this.loc = l; } //对象数组的setter方法 public void setemp(emp[] e){ this.emps = e ; } //getter方法 public int getdeptno(){ return deptno; } public string getdname(){ return dname; } public string getloc(){ return loc; } //对象数组的getter方法 public emp[] getemp(){ return emps; } //取得完整信息 public string getinfo(){ return "部门编号:" + this.deptno + ",名称:" + this.dname + ",位置:" + this.loc; }}//员工类class emp{ private int empno; private string ename ; private string job ; private double sal; private double comm ; private dept dept;//“引用关系” 在雇员里面保存部门信息 private emp mgr;//“引用关系” 在雇员里面设置领导信息 public emp(){//无参构造 } public emp(int empno,string ename, string job, double sal, double comm){//有参构造 this.empno = empno; this.ename = ename ; this.job = job; this.sal = sal ; this.comm = comm ; } //setter方法 public void setempno(int e){ this.empno = e; } public void setename(string n){ this.ename = n ; } public void setjob(string j){ this.job = j ; } public void setsal(double s){ this.sal = s ; } public void setcomm(double c){ this.comm = c ; } public void setmgr(emp m){ this.mgr = m; } public void setdept(dept d){ this.dept = d; } //getter方法 public int getempno(){ return empno; } public string getename(){ return ename; } public string getjbo(){ return job; } public double getsal(){ return sal; } public double getcomm(){ return comm; } public emp getmgr(){ return mgr; } public dept getdept(){ return dept; } public string getinfo(){//取得完整信息 return "雇员编号:" + this.empno + ",姓名:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:" + this.comm ; }}//测试类,设置数据并取出public class testdemo { public static void main(string[] args) { // 产生各自的独立对象,设置数据 dept dept = new dept(10,"accounting","new york"); emp ea = new emp(1234,"yiyao","clerk",900.0,0.0); emp eb = new emp(1235,"acan", "manager",4000.0,9.0); emp ec = new emp(1236,"dramatic","president",9000.0,80.0); //设置雇员和领导关系 ea.setmgr(eb); eb.setmgr(ec); //设置雇员和部门关系 ea.setdept(dept); eb.setdept(dept); ec.setdept(dept); //部门与雇员 dept.setemp(new emp[]{ea,eb,ec}); //取得数据 system.out.println(ea.getinfo()); system.out.println("\t|-" + ea.getmgr().getinfo()); system.out.println("\t|-" + ea.getdept().getinfo()); system.out.println("-----------------------------------------");//分割线 system.out.println(dept.getinfo()); for(int x = 0; x < dept.getemp().length; x++){ system.out.println("\t|-" + dept.getemp()[x].getinfo()); if(dept.getemp()[x].getmgr() != null){ system.out.println("\t\t|-" + dept.getemp()[x].getmgr().getinfo()); } } }}
相关文章:
java 基础 byte[]与各种数据类型互相转换的简单示例
从java的类型转换看mysql和oracle中的隐式转换
以上就是数据表和简单的java转换 _引用关系的实战案例的详细内容。