【发布时间】:2017-09-24 17:34:08
【问题描述】:
我希望在访问课程时能够看到任何学生的班级,并且我还希望在访问班级时能够拥有学生列表。
例子:
public class ClassRoom {
public String nome;
public List<Student> students;
public ClassRoom(String nome){
this.nome=nome;
}}
和
public class Student {
public String nome;
public ClassRoom aClass;
public Student(String nome, ClassRoom aClass){
this.nome = nome;
this.aClass = aClass;
}}
和
public School(){
ClassRoom c1 = new ClassRoom("English 102");
Student s1 = new Student("Anna White", c1);
Student s2 = new Student("Beatrix Blue", c1);
Student s3 = new Student("Carlos Brown", c1);
Student s4 = new Student("Dayana Green", c1);
Student s5 = new Student("Elliot Red", c1);
Student s6 = new Student("Jefferson Pink", c1);
c1.students = new ArrayList<>();
c1.students.add(s1);
c1.students.add(s2);
c1.students.add(s3);
c1.students.add(s4);
c1.students.add(s5);
c1.students.add(s6);
//example
System.out.println(
c1.students.get(1).aClass.students.get(2).aClass.students.get(3).nome
);
}}
这样编程有错吗?有没有更好的办法?
【问题讨论】:
标签: java android redundancy