范例讲解多对关系_第1页
范例讲解多对关系_第2页
范例讲解多对关系_第3页
范例讲解多对关系_第4页
范例讲解多对关系_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

范例-多对多关系赵翔宇一个学生可以选多门课程,一门课程可以有多个学生参加,那么这就是一个典型的多对多关系。要完成本程序,首先应该定义两个类:学生信息类Student、课程信息类Course,在一个学生类中存在一个集合,保存全部的课程,而在课程类中也要存在一个集合,保存全部的学生。实例:定义学生类classStudent{privateStringname;privateintage;privateList<Course>allCourse;publicStudent(Stringname,intage){this();this.setName(name);this.setAge(age);}publicStudent(){allCourse=newArrayList<Course>();}publicStringgetName(){returnname;}publicvoidsetName(Stringname){=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringtoString(){return"学生姓名:"++";年龄:"+this.age;}publicList<Course>getAllCourse(){returnallCourse;}publicvoidsetAllCourse(List<Course>allCourse){this.allCourse=allCourse;}}在学生类中存在一个allCourse的List集合,这样在程序运行时,一个学生类中可以保存多个Course类对象。实例:定义课程类classCourse{privateStringname;privateintcredit;privateList<Student>allStudents;publicCourse(){allStudents=newArrayList<Student>();}publicCourse(Stringname,intcredit){this();this.setName(name);this.setCredit(credit);}publicStringgetName(){returnname;}publicvoidsetName(Stringname){=name;}publicintgetCredit(){returncredit;}publicvoidsetCredit(intcredit){this.credit=credit;}publicList<Student>getAllStudents(){returnallStudents;}publicvoidsetAllStudents(List<Student>allStudents){this.allStudents=allStudents;}publicStringtoString(){return"课程名称:"++";课程学分:"+this.credit;}}课程类和学生类一样,都定义了一个List集合,用于保存多个学生信息。实例:测试程序publicclassTest{publicstaticvoidmain(String[]args){Coursec1=newCourse("英语",5);Coursec2=newCourse("计算机",10);Students1=newStudent("张三",21);Students2=newStudent("李四",22);Students3=newStudent("王五",23);Students4=newStudent("赵六",24);Students5=newStudent("孙七",25);//第一门课有三人参加c1.getAllStudents().add(s1);c1.getAllStudents().add(s2);c1.getAllStudents().add(s3);s1.getAllCourse().add(c1);s2.getAllCourse().add(c1);s3.getAllCourse().add(c1);//第二门有四个人参加c2.getAllStudents().add(s2);c2.getAllStudents().add(s3);c2.getAllStudents().add(s4);c2.getAllStudents().add(s5);s2.getAllCourse().add(c2);s3.getAllCourse().add(c2);s4.getAllCourse().add(c2);s5.getAllCourse().add(c2);//输出第一门看多少人参加System.out.println(c1);Iterator<Student>iter=c1.getAllStudents().iterator();while(iter.hasNext()){System.out.println("\t-|"+iter.next());}//输出一个学生参加的课程信息System.out.println(s2);Iterator<Course>iter2=s2.getAllCourse().iterator();while(iter2.hasNext()){System.out.println("\t-|"+iter2.next());}}}程序运行结果:课程名称:英语;课程学分:5-|学生姓名:张三;年龄:21-|学生姓名:李四;年龄:22-|学生姓名:王五;年龄:23学生姓名:李四;年龄:22-|课程名称:英语;课程学分:5-|课程名称:计算机

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论