Java example-many relationship

Using collections can only represent one-to-many relationship can also be expressed. For example, a student can choose many courses, a course can have more students, then this is a typical-many relationship.

To complete the above requirements, should first define two categories, namely student information (Student) class, course information (Course) class. In the presence of students in a class collection, save all courses. Similarly, in the course there is also a collection of classes, save all students.
1. The definition of student class

public class Student {
    private String name;
    private int age;
    private List<Course> allCourses; // 定义集合保存全部课程

    private Student() {
        this.allCourses = new ArrayList<Course>();// 实例化List集合
    }

    // 通过构造方法设置内容
    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;
    }

    // 重写toString()方法
    public String toString() {
        return "学生姓名:" + this.name + ":年龄" + this.age;
    }
}

There is a student in the class allCourses List collection, so the program is running, a student in the class can save multiple Course objects.

2. The definition of the class curriculum

public class Course {
    private String name;
    private int credit;
    // 定义集合保存多个学生
    private List<Student> allStudents;

    private Course() {
        // 实例化List集合
        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;
    }

    // 重写toString()方法
    public String toString() {
        return "课程名称" + this.name + ";课程学分" + this.credit;
    }
}

Coursework and student class, defines a List collection to save multiple student information.

3. Test Program

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);
        // 第一门课程有3个人参加,向课程中增加3个学生信息,同时向学生中增加课程信息
        c1.getAllStudents().add(s1);
        c1.getAllStudents().add(s2);
        c1.getAllStudents().add(s6);
        s1.getAllCourses().add(c1);
        s2.getAllCourses().add(c1);
        s6.getAllCourses().add(c1);
        // 第二门课程有6个人参加,向课程中增加6个学生信息,同时向学生中添加课程信息
        // 向课程中增加学生信息
        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);
        }
    }
}

Output:

课程名称英语;课程学分3
    学生姓名:张三:年龄20
    学生姓名:李四:年龄21
    学生姓名:钱八:年龄25
学生姓名:钱八:年龄25
    课程名称英语;课程学分3
    课程名称计算机;课程学分5

Program uses a two-way deal with the relationship, so students in the choice of a course, in addition to courses you want to add students, but also add course information among students. When output course information, students can find all the information to participate in this course by course object collection, you can also find information about all courses attended by students.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104718195