Java-to-many relationship example

Common life-many relationship examples, such as a school may include more than one student, a student belonging to a school, then this is a typical-many relationship, the relationship can be expressed through the collection.

1. The definition of student class

import java.util.HashSet;
import java.util.Iterator;

public class Student {
    private String name; // 定义student类
    private int age; // 定义name属性
    private School school; // 一个学生属于一个学校

    // 通过构造方法设置内容
    public Student(String name, int age) {
        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 School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

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

School property includes a more Student class represents a student belonging to a school. The program is running, only need to pass a reference School classes can be completed such a relationship.

2. Definitions school class

import java.util.ArrayList;
import java.util.List;

public class School {
    private String name;
    private List<Student> allStudents; // 一个学校有多个学生

    public School() {
        this.allStudents = new ArrayList<Student>();// 实例化List集合
    }

    public School(String name) {
        this();
        this.setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Student> getAllStudents() {
        return allStudents;
    }

    public void setAllStudents(List<Student> allStudents) {
        this.allStudents = allStudents;
    }

    // 重写toString()方法
    public String toString() {
        return "学校名称:" + this.name;
    }
}

When school class definitions defines a type attribute List, and specify the type Student generic type is, such an action would represent a School object contains references to a plurality of types of Student.

3. Test the code, set relations

import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        // 实例化学校对象
        School sch = new School("背景大学");
        // 实例化学生对象
        Student s1 = new Student("张三", 21);
        Student s2 = new Student("李四", 22);
        Student s3 = new Student("王五", 23);
        // 在学校中加入学生
        sch.getAllStudents().add(s1);
        sch.getAllStudents().add(s2);
        sch.getAllStudents().add(s3);
        // 一个学生属于一个学校
        s1.setSchool(sch);
        s2.setSchool(sch);
        s3.setSchool(sch);
        // 输出学校信息
        System.out.println(sch);

        // 实例化Iterator对象,用于输出全部的学生信息
        Iterator<Student> ite = sch.getAllStudents().iterator();
        while (ite.hasNext()) {
            System.out.println("\t" + ite.next());
        }
    }
}

Program results are as follows:

学校名称:北京大学
    学生姓名:张三:年龄21
    学生姓名:李四:年龄22
    学生姓名:王五:年龄23

This code to each object instantiated School and Student class, then stored reference relationships with each other by the properties of two classes, thereby forming a plurality of a school student, a student belonging to a school-many relationship.

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

Guess you like

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