继我的这个问题之后:制作这个 Java 程序的最佳方法是什么?
建议我在 Lecturer 类和 Course 类中存储一个列表。所以我做到了,它看起来像这样:
public class Lecturer
{
private String id;
private String name;
List<Course> courses = new ArrayList<Course>(); // a list to hold the courses
public Lecturer(String idIn, String nameIn) // arguments of the constructor
{
id = idIn;
name = nameIn;
}
}
除了有讲师列表之外,课程课程也是如此。但我不明白的是,在那里放置一个列表究竟有什么作用?因为我不知道在哪里放置 ArrayList 的方法,例如从中添加和删除讲师?
有人可以解释这样做的目的吗?
我使用另一种方法,该方法基本上是将数组列表及其方法放在讲师和课程的两个单独的类中,然后我只需将其作为属性添加到课程和讲师类中,例如:
public class Lecturer
{
private String id;
private String name;
private CourseList courses; // COurseList is the class with the arraylist and methods
public Lecturer(String idIn, String nameIn) // arguments of the constructor
{
id = idIn;
name = nameIn;
courses = new CourseList();
}
}
我希望我是有道理的,因为过去两周我一直坚持一件事,这似乎没有人能理解。
谢谢