어떤 학생이든 액세스 할 때 수업을 볼 수 있기를 원하며 수업에 액세스 할 때 학생 목록을 가질 수 있기를 원합니다.첫 번째 객체를 참조하는 객체를 다른 객체 내부에서 참조하고자합니다. 이로 인해 어떤 문제가 발생할 수 있습니까?
예 :
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
);
}}
가 이런 식으로 프로그램을 잘못입니까? 더 좋은 방법이 있습니까?