Java类集 _范例讲解:多对多关系
2011-09-14 11:06
323 查看
实例要求
一个学生可以选多门课程, 门课程可以有多个学生参加,那么这就是一个典型的多对多关系。
要完成本程序,首先应该定义两个类:学生信息类Student、课程信息类Course,在一个学生类中存在一个集合,保存全部的课程,而在课程类中也要存在一个集合,保存全部的学生。
实例主要采用的知识
1、List 集合
2、引用传递
3、Iteratro
学生类:
1、此处 多对多关系只是在这种单独类,这样的类可以表示成实体类。只是在这上面设置的关系。
一个学生可以选多门课程, 门课程可以有多个学生参加,那么这就是一个典型的多对多关系。
要完成本程序,首先应该定义两个类:学生信息类Student、课程信息类Course,在一个学生类中存在一个集合,保存全部的课程,而在课程类中也要存在一个集合,保存全部的学生。
实例主要采用的知识
1、List 集合
2、引用传递
3、Iteratro
学生类:
import java.util.List ; import java.util.ArrayList ; public class Student{ private String name ; private int age ; private List<Course> allCourses ; public Student(){ this.allCourses = new ArrayList<Course>() ; } public Student(String name,int age){ this() ; this.name = name ; this.age = age ; } public List<Course> getAllCourses(){ return this.allCourses ; } public void setName(String name){ this.name = name ; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public int getAge(){ return this.age ; } public String toString(){ return "学生姓名:" + this.name + ";年龄:" + this.age ; } };课程类:
import java.util.List ; import java.util.ArrayList ; public class Course{ private String name ; private int credit ; private List<Student> allStudents ; public Course(){ this.allStudents = new ArrayList<Student>() ; } public Course(String name,int credit){ this() ; this.name = name ; this.credit = credit ; } public List<Student> getAllStudents(){ return this.allStudents ; } public void setName(String name){ this.name = name ; } public void setCredit(int credit){ this.credit = credit ; } public String getName(){ return this.name ; } public int getCredit(){ return this.credit ; } public String toString(){ return "课程名称:" + this.name + ";课程学分:" + this.credit ; } };关系测试类:
import java.util.Iterator ; public class TestMore{ public static void main(String args[]){ Course c1 = new Course("英语",3 ) ; // 第一门课程 Course c2 = new Course("计算机",5) ; // 第二门课程 Student s1 = new Student("张三",20) ; Student s2 = new Student("李四",21) ; Student s3 = new Student("王五",22) ; Student s4 = new Student("赵六",23) ; Student s5 = new Student("孙七",24) ; Student s6 = new Student("钱八",24) ; // 第一门课程有三个学生参加 c1.getAllStudents().add(s1) ; c1.getAllStudents().add(s2) ; c1.getAllStudents().add(s6) ; s1.getAllCourses().add(c1) ; s2.getAllCourses().add(c1) ; s6.getAllCourses().add(c1) ; // 第二门课程有六个学生参加 c2.getAllStudents().add(s1) ; c2.getAllStudents().add(s2) ; c2.getAllStudents().add(s3) ; c2.getAllStudents().add(s4) ; c2.getAllStudents().add(s5) ; c2.getAllStudents().add(s6) ; s1.getAllCourses().add(c2) ; s2.getAllCourses().add(c2) ; s3.getAllCourses().add(c2) ; s4.getAllCourses().add(c2) ; s5.getAllCourses().add(c2) ; s6.getAllCourses().add(c2) ; // 输出一门课程的信息,观察一门课程有多少个学生参加\ System.out.println(c1) ; Iterator<Student> iter1 = c1.getAllStudents().iterator() ; while(iter1.hasNext()){ Student s = iter1.next() ; System.out.println("\t|- " + s) ; } // 通过学生找到学生参加的课程 System.out.println(s6) ; Iterator<Course> iter2 = s6.getAllCourses().iterator() ; while(iter2.hasNext()){ Course c = iter2.next() ; System.out.println("\t|- " + c) ; } } };总结:
1、此处 多对多关系只是在这种单独类,这样的类可以表示成实体类。只是在这上面设置的关系。
相关文章推荐
- 〖Java类集〗_范例讲解:一对多关系笔记
- 〖Java类集〗_范例讲解:多对多关系笔记
- Java类集 _范例讲解:一对多关系
- 〖Java类集〗_范例讲解:多对多关系笔记
- Java类集学习(六)应用范例(一对多的关系)
- Java类集 - 一对多关系范例
- Java类集学习(七)应用范例(多对多的关系)
- 〖Java类集〗_范例讲解:一对多关系笔记
- java中类与类之间的关系讲解
- Java学习之范例与类集总结
- java:类集操作,多对多的关系
- 范例讲解:多对多的关系
- Java类集-多对多关系范例
- _范例讲解:一对多关系
- Java第二课 Java面向对象编程,面向对象编程和面向过程编程的区别,我们如何才能掌握面向对象的编程,类和对象的关系;讲解了Java中的构造、重载、this和super变量、静态变量、Java中的常
- Java类集(接口继承的关系)
- 类集范例(一对多关系和多对多关系)
- java类集框架小结
- 细讲解JAVA中的IO流
- Java实现Web应用中的定时任务(实例讲解)