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

2020-12-25 16:28

阅读:700

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

技术图片

 

 

school类:

package setdone;

import java.util.List;

public class School {
    private String schoolname;
    private List sch_list;

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

    public String getSchoolname() {
        return schoolname;
    }

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

    public List getSch_list() {
        return sch_list;
    }

    public void setSch_list(List 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 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


评论


亲,登录后才可以留言!