java学校学校_java:练习学校学生

java:练习学校学生

一个学生对应一个学校

一个学校对应多个学生

Student类,School类,Demo测试类

Student:

public class Student {

private String name;

private int age;

private School school;

public Student() {

super();

}

public Student(String name, int age) {

this.name = name;

this.age = 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;

}

public String toString()

{

return "学生姓名:"+this.name+",学生年龄"+this.age;

}

}

School类

public class School {

private String name;

private List allStudents;

public School()

{

this.allStudents = new ArrayList();

}

public School(String name)

{

this();

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public List getAllStudents() {

return allStudents;

}

public String toString()

{

return "学校信息:" + this.name;

}

}

测试;

//一个学生对应一个学校

//一个学校对应多个学生

School school = new School("zhdzdx");

Student stu1 = new Student("张三",22);

Student stu2 = new Student("李四",33);

Student stu3 = new Student("王五",22);

school.getAllStudents().add(stu1);

stu1.setSchool(school);

school.getAllStudents().add(stu2);

stu2.setSchool(school);

school.getAllStudents().add(stu3);

stu3.setSchool(school);

System.out.println(school);

Iterator iter = school.getAllStudents().iterator();

while(iter.hasNext())

{

Student stu = (Student) iter.next();

System.out.println(stu);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值