一对多
生活中常见一对多关系的例子,如一个老师教多个学生,一个学生属于一个老师,那么这就是一个典型的一对多关系,可以通过集合进行关系的表示。
定义一个学生类
public class Student { private Integer id; private String name; private byte age; private Teacher teacher; public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Student(Integer id, String name, byte age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public byte getAge() { return age; } public void setAge(byte age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age +'}'; } } 在以上的 Student 类中包含了一个 Teacher 属性,表示一个学生属于一个老师。在程序运行时,只需要传入 Teacher 类的引用就可以完成这样的关系。
定义一个老师类
import java.util.HashSet; public class Teacher { private Integer id; private String name; private byte sex; private HashSet<Student> students = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public byte getSex() { return sex; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + ", sex=" + sex + ", students=" + students + '}'; } public Teacher(Integer id, String name, byte sex) { this.id = id; this.name = name; this.sex = sex; } public void setSex(byte sex) { this.sex = sex; } public HashSet<Student> getStudents() { return students; } public void setStudents(HashSet<Student> students) { this.students = students; } }
逻辑关系
//一对多 public class OneToManyDemo { public static void main(String[] args) { Student heyi1 = new Student(1, "heyi1", (byte) 2); Student heyi2 = new Student(2, "heyi2", (byte) 4); Teacher teacher = new Teacher(1, "heyi老师", (byte) 12); HashSet<Student> students = teacher.getStudents(); students.add(heyi1); students.add(heyi2); heyi2.setTeacher(teacher); heyi1.setTeacher(teacher); print(teacher); } private static void print(Teacher t){ System.out.println(t.getName()); for (Student s: t.getStudents()) { System.out.println(s); } } }
多对多
使用集合不仅可以表示一对一的关系,也可以表示多对多的关系。例如,一个学生可以选多门课程,一门课程可以有多个学生参加,那么这就是一个典型的多对多关系。
要完成上面要求,首先应该定义两个类,分别是学生信息(Student)类、课程信息(Course)类。在学生类中存在一个集合,保存全部的课程。同样,在课程类中也要存在一个集合,保存全部的学生。
定义学生类
package model.vo; import java.util.HashSet; public class Student { private Integer id; private String name; private byte age; private Teacher teacher; private HashSet<Course> allCourse = new HashSet<Course>(); public HashSet<Course> getAllCourse() { return allCourse; } public void setAllCourse(HashSet<Course> allCourse) { this.allCourse = allCourse; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Student(Integer id, String name, byte age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public byte getAge() { return age; } public void setAge(byte age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age +'}'; } }
定义课程类
package model.vo; import java.util.HashSet; public class Course { private int id; //课程名称 private String name; private HashSet<Student> allStudents = new HashSet<>(); public Course(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public HashSet<Student> getAllStudents() { return allStudents; } public void setAllStudents(HashSet<Student> allStudents) { this.allStudents = allStudents; } @Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
逻辑关系
import model.vo.Course; import model.vo.Student; import model.vo.Teacher; public class ManyToManyDemo { public static void main(String[] args) { Course c1 = new Course(3, "英语"); // 实例化课程对象 Course c2 = new Course(5, "计算机"); // 实例化课程对象 Student s1 = new Student(1, "张三", (byte) 20); // 实例化学生对象 Student s2 = new Student(2, "李四", (byte) 21); Student s3 = new Student(3, "王五", (byte) 22); // 向课程增加学生信息 c1.getAllStudents().add(s1); c1.getAllStudents().add(s2); // 向学生信息增加课程 s1.getAllCourse().add(c1); s1.getAllCourse().add(c2); coursePrint(c1); studentPrint(s1); } private static void coursePrint(Course course) { System.out.println(course.getName()); for (Student s : course.getAllStudents()) { System.out.println(s); } } private static void studentPrint(Student student) { System.out.println(student.getName()); for (Course course : student.getAllCourse()) { System.out.println(course); } } }