使用集合不仅可以表示一对一的关系,也可以表示多对多的关系。例如,一个学生可以选多门课程,一门课程可以有多个学生参加,那么这就是一个典型的多对多关系。
要完成上面要求,首先应该定义两个类,分别是学生信息(Student)类、课程信息(Course)类。在学生类中存在一个集合,保存全部的课程。同样,在课程类中也要存在一个集合,保存全部的学生。
1)定义学生类
-
public class Student {
-
private String name;
-
private int age;
-
private List<Course> allCourses;
-
-
private Student() {
-
this.allCourses = new ArrayList<Course>();
-
}
-
-
-
public Student(String name, int age) {
-
-
this();
-
this.setName(name);
-
this.setAge(age);
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
public List<Course> getAllCourses() {
-
return allCourses;
-
}
-
-
public void setAllCourses(List<Course> allCourses) {
-
this.allCourses = allCourses;
-
}
-
-
-
public String toString() {
-
return "学生姓名:" + this.name + ":年龄" + this.age;
-
}
-
}
在学生类中存在一个 allCourses 的 List 集合,这样在程序运行时,一个学生类中可以保存多个 Course 对象。
2)定义课程类
-
public class Course {
-
private String name;
-
private int credit;
-
-
private List<Student> allStudents;
-
-
private Course() {
-
-
this.allStudents = new ArrayList<Student>();
-
}
-
-
public Course(String name, int credit) {
-
this();
-
this.setName(name);
-
this.setCredit(credit);
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public int getCredit() {
-
return credit;
-
}
-
-
public void setCredit(int credit) {
-
this.credit = credit;
-
}
-
-
public List<Student> getAllStudents() {
-
return allStudents;
-
}
-
-
public void setAllStudents(List<Student> allStudents) {
-
this.allStudents = allStudents;
-
}
-
-
-
public String toString() {
-
return "课程名称" + this.name + ";课程学分" + this.credit;
-
}
-
}
课程类与学生类一样,都定义了一个 List 集合,用于保存多个学生信息。
3)测试程序
-
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("钱八", 25);
-
-
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);
-
}
-
}
-
}
输出结果如下:
课程名称英语;课程学分3 学生姓名:张三:年龄20 学生姓名:李四:年龄21 学生姓名:钱八:年龄25 学生姓名:钱八:年龄25 课程名称英语;课程学分3 课程名称计算机;课程学分5
从程序来看,设计关系的地方较为复杂,因为现在的程序采用的是双向的处理关系,所以学生在选择一个课程时,除了课程中要添加学生,在学生中也要添加课程信息。在输出课程信息时,可以通过课程对象中的集合找到参加此课程的全部学生信息,也可以通过学生找到全部参加的课程信息。