//学校类 class School { //多个班级 private Class[] aClass; private String schoolName; public void School() { } public Class[] getaClass() { return aClass; } public void setaClass(Class[] aClass) { this.aClass = aClass; } public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } } //班级类class Class { //多个学生 private Student[] student; private String aClassName; public void Class() { } public Student[] getStudent() { return student; } public void setStudent(Student[] student) { this.student = student; } public String getaClassName() { return aClassName; } public void setaClassName(String aClassName) { this.aClassName = aClassName; } } //学生类class Student { private String name; private int age; public void Student() { } 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 class Test { public static void main(String[] args) { School school=new School(); school.setaClass(new Class[10]); for (int i=0;i<10;i++){ school.getaClass()[i]=new Class(); school.getaClass()[i].setStudent(new Student[30]); school.getaClass()[i].setaClassName("班级"+i); for (int j=0;j<30;j++){ school.getaClass()[i].getStudent()[j]=new Student(); school.getaClass()[i].getStudent()[j].setAge(j+10); school.getaClass()[i].getStudent()[j].setName("学生"+j); } } for (int i=0;i<10;i++){ for (int j=0;j<30;j++){ System.out.println("班级:"+school.getaClass()[i].getaClassName()+" 名字:"+school.getaClass()[i].getStudent()[j].getName()+" 年龄"+school.getaClass()[i].getStudent()[j].getAge()); } } } }
java学校,班级,学生类的三级关系
最新推荐文章于 2024-10-12 20:13:12 发布