码迷,mamicode.com
首页 > 编程语言 > 详细

java-集合-realdo-集合一对多

时间:2020-06-03 17:40:19      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:override   setname   alt   new   img   大学   set   oid   com   

技术图片

 

 

school类:

package setdone;

import java.util.List;

public class School {
    private String schoolname;
    private List<Student> sch_list;

    public School(String schoolname) {
        this.schoolname = schoolname;
    }

    public String getSchoolname() {
        return schoolname;
    }

    public void setSchoolname(String schoolname) {
        this.schoolname = schoolname;
    }

    public List<Student> getSch_list() {
        return sch_list;
    }

    public void setSch_list(List<Student> sch_list) {
        this.sch_list = sch_list;
    }
}

 

student 类:

package setdone;

public class Student {
    private String name;
    private String school;
    private int age;

    public Student(String name,  int age) {
        this.name = name;
        this.school = school;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getSchool() {
        return school;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=‘" + name + \‘ +
                ", school=‘" + school + \‘ +
                ", age=" + age +
                };
    }
}

test

package setdone;

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

public class SsTest {
    public static void main(String[] args) {
        Student student1 = new Student("张三",22);
        Student student2 = new Student("李四",42);
        Student student3 = new Student("王五",23);

        School school = new School("清华大学");

        student1.setSchool(school.getSchoolname());
        student2.setSchool(school.getSchoolname());
        student3.setSchool(school.getSchoolname());

        List<Student> sch_stu = new ArrayList<>();
        sch_stu.add(student1);
        sch_stu.add(student2);
        sch_stu.add(student3);

        school.setSch_list(sch_stu);
        System.out.println("学校名称:" + school.getSchoolname());
        for (Student student:school.getSch_list()) {
            System.out.println("学生" + student);
        }
    }


}

 

java-集合-realdo-集合一对多

标签:override   setname   alt   new   img   大学   set   oid   com   

原文地址:https://www.cnblogs.com/java-quan/p/13038583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!