//学校类 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()); } } } }
package school;
public class CalcStudent {
public static void main(String[] args) {
School school = new School("北京大学");
Clazz cla1 = new Clazz(202101,"金融202101班",school);
Clazz cla2 = new Clazz(202102,"金融202102班",school);
Student s1 = new Student(1001, "张三", "男", 21, cla1);
Student s2 = new Student(1002, "丽丽", "女", 20, cla1);
Student s3 = new Student(1003, "李四", "男", 22, cla2);
Student s4 = new Student(1004, "黄蓉", "女", 19, cla2);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("全校共有"+Student.count+"人.");
}
}
class School{
String schoolName;
public School() {}
public School(String schoolName) {
this.schoolName = schoolName;
}
@Override
public String toString() {
return "学校名称是:"+this.schoolName;
}
}
class Clazz{
int classId;
String className;
School school;
public Clazz() {}
public Clazz(int classId,String className,School school) {
this.classId = classId;
this.className = className;
this.school = school;
}
@Override
public String toString() {
return "班级名称是:"+this.className+",学校:" + school.schoolName;
}
}
class Student{
int studId;
String studName;
String sex;
int age;
Clazz cls;
static int count;
public Student() {
count++;
}
public Student(int studId,String studName,String sex,int age,Clazz cls) {
this.studId = studId;
this.studName = studName;
this.sex = sex;
this.age = age;
this.cls = cls;
count++;
}
@Override
public String toString() {
return "学号:"+this.studId+",姓名:"+this.studName +",班级:"+cls.className +",学校"+cls.school.schoolName;
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632