数据表与简单Java类(多对多的关系)

 

  1 public class Newbegin{
  2 public static void main(String args[]) {
  3  //第一步:根据结构进行关系设置
  4  //1.创建各自的独立对象
  5  Student stu1=new Student(1,"张三",18);
  6  Student stu2=new Student(2,"李四",19);
  7  Student stu3=new Student(3,"王五",18);
  8  Course ca=new Course(1001,"马克思主义哲学",3); 
  9  Course cb=new Course(1002,"操作系统",2);
 10  //2.设置学生和课程的关系,这里面需要准备出成绩
 11  stu1.setStudentCourse(new StudentCourse[]{
 12   new StudentCourse(stu1,ca,99.9),
 13   new StudentCourse(stu1,cb,87.0),
 14  });
 15  stu2.setStudentCourse(new StudentCourse[]{
 16   new StudentCourse(stu2,ca,78.9),
 17  });
 18  stu3.setStudentCourse(new StudentCourse[]{
 19   new StudentCourse(stu3,cb,98.0),
 20  });
 21  //3.设置课程和学生的关系
 22  ca.setStudentCourse(new StudentCourse[]{
 23   new StudentCourse(stu1,ca,99.9),
 24   new StudentCourse(stu2,ca,78.9)
 25  });
 26  cb.setStudentCourse(new StudentCourse[]{
 27   new StudentCourse(stu1,cb,87.0),
 28   new StudentCourse(stu3,cb,98.9)
 29  });
 30  //第二部:根据结构取出数据
 31  //1.可以找到一门课程,以及参加此课程的所以学生信息,和他的成绩;
 32  System.out.println(ca.getInfo());
 33  for(int x=0;x<ca.getStudentCourse().length;x++){
 34   System.out.print("	|-"+ca.getStudentCourse()[x].getStudent().getInfo()); 
 35   System.out.print(",成绩:"+ca.getStudentCourse()[x].getScore());
 36   System.out.println();
 37  }
 38  //2.可以找到一名学生,以及参加的课程信息,和他的成绩
 39  System.out.println("========================");
 40  System.out.println(stu1.getInfo());
 41  for(int x=0;x<stu1.getStudentCourse().length;x++){
 42   System.out.print("	|-"+stu1.getStudentCourse()[x].getCourse().getInfo());
 43   System.out.print(",成绩:"+stu1.getStudentCourse()[x].getScore());
 44   System.out.println();
 45  }
 46 }
 47 }
 48 class Student
 49 {
 50  private int stuid; 
 51  private String name;
 52  private int age;
 53  private StudentCourse studentCourse[];
 54  public Student(){}
 55  public Student(int stuid,String name,int age){
 56   this.stuid=stuid;
 57   this.name=name;
 58   this.age=age;
 59  }
 60  public void setStudentCourse(StudentCourse studentCourse[]){
 61   this.studentCourse=studentCourse;
 62  }
 63  public StudentCourse[] getStudentCourse(){
 64   return this.studentCourse;
 65  }
 66  public String getInfo(){
 67   return "【STUDENT】学生编号:"+this.stuid+",姓名:"+this.name+",年龄:"+this.age;
 68  }
 69 }
 70 class Course
 71 {
 72  private int cid;
 73  private String name;
 74  private int credit;
 75  private StudentCourse studentCourse[];
 76  public Course(){} 
 77  public Course(int cid,String name,int credit){
 78   this.cid=cid;
 79   this.name=name;
 80   this.credit=credit;
 81  }
 82  public void setStudentCourse(StudentCourse studentCourse[]){
 83   this.studentCourse=studentCourse;
 84  }
 85  public StudentCourse[] getStudentCourse(){
 86   return this.studentCourse;
 87  }
 88  public String getInfo(){
 89   return "【COURSE】课程编号:"+this.cid+",名称:"+this.name+",学分:"+this.credit;
 90  }
 91 }
 92 class StudentCourse//学生选课
 93 {
 94  private Student student;
 95  private Course course;
 96  private double score; 
 97  public StudentCourse(){}
 98  public StudentCourse(Student student,Course course,double score){
 99   this.student=student;
100   this.course=course;
101   this.score=score;
102  }
103  public Student getStudent(){
104   return this.student;
105  }
106  public Course getCourse(){
107   return this.course;
108  }
109  public double getScore(){
110   return this.score;
111  }
112 }

 

 

 

 

原文地址:https://www.cnblogs.com/Tony98/p/10388995.html