【发布时间】:2009-04-20 19:07:00
【问题描述】:
继续我的这个Q:What's the best way to make this Java program?
建议我在 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;
}
}
除了有讲师列表外,Course 课程也是如此。但我不明白的是,在那里放置一个列表到底有什么作用?因为我不知道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();
}
}
我希望我是有道理的,因为过去 2 周以来我一直坚持一件事,似乎没有人能理解。
谢谢
【问题讨论】:
标签: java list attributes arraylist