class Emp{ private int empno;//職工編號 private String ename;//姓名 private String job;//職位 private double sal;//基本工資 private double comm; private Emp mgr;//所屬領導 private Dept dept;//所在部門 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; } public void setMgr(Emp mgr) {//傳遞領導信息 this.mgr = mgr; } public Emp getMgr(){//獲取領導信息 return this.mgr; } public void setDept(Dept dept) {//設置部門信息 this.dept = dept; } public Dept getDept() {//讀取部門信息 return dept; } public String getInfo(){ return "職工編號:"+this.empno +",職工姓名:" + this.ename + ",職工職位:" + this.job + ",基本工資:" + this.sal +",提成:" + this.comm; } } 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; } public void setEmps(Emp[] emps) { this.emps = emps; } public Emp[] getEmps() { return emps; } public String getInfo(){ return "部門編號:" + this.deptno +",部門名稱:" + this.dname +",所在地址:" + this.loc; } } public class Main { public static void main(String[] args) { // 創建各自的實例化對象 Dept dept = new Dept(10,"ACCOUNTING","NEW YORK"); Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0); Emp eb = new Emp(7466,"ALLEN","MANAGER",2450.0,0.0); Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0); //設置領導關系 ea.setMgr(eb); eb.setMgr(ec);//ec對象沒有領導,因為他是頭 //設置員工和部門關系 ea.setDept(dept); eb.setDept(dept); ec.setDept(dept); //設置部門和員工的關系 dept.setEmps(new Emp[]{ea,eb,ec}); //讀取數據 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"+dept.getEmps()[x].getMgr().getInfo()); } } System.out.println("************************"); //根據員工找到對應的領導信息和部門信息 System.out.println(eb.getInfo()); if(eb.getMgr()!=null) System.out.println("\t"+eb.getMgr().getInfo()); if(eb.getDept()!=null) System.out.println("\t"+eb.getDept().getInfo()); } }
上面代碼是實現一對多的數據表,基於公司人事管理的例子(根據員工可以輸出其基本信息及所在部門信息和所屬領導信息,根據部門可以輸出所有員工及領導),在Java一對多的數據關系中,需要遵循以下設計原則:
簡單Java類設計原則:Java類與數據表的關系
1、Java的名稱 = 實體表的名稱
2、Java類的屬性 = 實體表的字段
3、Java類的一個對象 = 表的一行記錄
4、對象數組 = 表的多行記錄
5、外鍵關系 = 引用配置
多對多的數據表(學生成績管理):根據學生可以輸出所修課程信息及成績,根據課程可以輸出學習該課程的學生信息及成績
class Student{//學生表 private int stuid; private String sname; private int age; private StudentCourse studentCourse[];//學生成績信息 public Student(){} public Student(int stuid,String sname,int age){ this.stuid = stuid; this.sname = sname; this.age = age; } public void setStudentCourse(StudentCourse[] studentCourse) { this.studentCourse = studentCourse; } public StudentCourse[] getStudentCourse() { return studentCourse; } public String getInfo(){ return "學號:" + this.stuid + ",學生姓名:" + this.sname + ",年齡:" + this.age; } } class Course{//課程表 private String name; private int cid; private int credit; private StudentCourse studentCourse[]; public Course(){} public Course(String name,int cid,int credit){ this.cid = cid; this.name = name; this.credit = credit; } public StudentCourse[] getStudentCourse() { return studentCourse; } public void setStudentCourse(StudentCourse[] studentCourse) { this.studentCourse = studentCourse; } public String getInfo(){ return "課號:" + this.credit + ",名稱:" + this.name + ",學分:" + this.credit; } } class StudentCourse{//學生選課表 private Student student; private Course course; private double score;//成績 public StudentCourse(){ } public StudentCourse(Student student,Course course,double score){ this.course = course; this.score = score; this.student = student; } public Course getCourse() { return course; } public Student getStudent() { return student; } public double getScore() { return score; } } public class Main { public static void main(String[] args) { //創建各自的獨立對象 Student stu1 = new Student(1,"張三",18); Student stu2 = new Student(1,"李四",20); Student stu3 = new Student(1,"王五",19); Course ca = new Course("高等數學",1001,5); Course cb = new Course("線性代數",1002,4); //設置各自的關系 //設置學生和課程的關系 stu1.setStudentCourse(new StudentCourse[]{ new StudentCourse(stu1,ca,92.5), new StudentCourse(stu1,cb,94.0) }); stu2.setStudentCourse(new StudentCourse[]{ new StudentCourse(stu2,ca,89.0) }); stu3.setStudentCourse(new StudentCourse[]{ new StudentCourse(stu3,cb,95.0), new StudentCourse(stu3,ca,90.5) }); //設置課程和學生的關系 ca.setStudentCourse(new StudentCourse[]{ new StudentCourse(stu1,ca,92.5), new StudentCourse(stu2,ca,89.0) new StudentCourse(stu3,ca,90.5) }); cb.setStudentCourse(new StudentCourse[]{ new StudentCourse(stu1,cb,94.0), new StudentCourse(stu3,cb,95.0) }); //找到一門課程,並且輸出學習此課程的所有學生的信息及成績 System.out.println(ca.getInfo()); for(int x = 0;x < ca.getStudentCourse().length;x++){ System.out.println("\t"+ca.getStudentCourse()[x].getStudent().getInfo() +",成績:"+ca.getStudentCourse()[x].getScore()); }; System.out.println("***********"); //根據學生輸出其信息及選修的課程信息和所得成績 System.out.println(stu1.getInfo()); for(int x = 0;x < stu1.getStudentCourse().length;x++){ System.out.println("\t選修課程:" + stu1.getStudentCourse()[x].getCourse().getInfo() + ",所得成績:" + stu1.getStudentCourse()[x].getScore()); } } }
