我正在尝试这样做。我有一个enum
个工作日。我使用enum
因为工作日是常数
public enum WeekDay {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
}
我有一个名为Session
的班级。会话就是在特定时间发生的事情,例如数学课
public class Session {
// some fields
public String title;
public int duration,start,end;
/** several methods follow to get and set, and check e.t.c **/
}
有一个名为Venue
的第三类。场地举办会议,例如数学课可以从上午9点到上午10点,在一个叫做“数学课”的场地(一个例子)
public class Venue { // simply a place that can hold several sessions in a day
private String name;
private int capacity;
/** several methods**/
}
我需要做的是 - 在枚举中创建会话列表,即每天都有会话,然后我需要在场地内的结构(ArrayList或enumset?)中保存枚举,即场地有周一至周五的课程(理想的是学校课程)。所以它会是这样的:
public enum WeekDay {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
/** the list of sessions for a given day**/
private ArrayList <Session> list;
private int numOfSessions; // number of sessions
/** with some methods like **/
addSession();
removeSession();
getSession();
checkTimeOfSession();
...
}
所以在场地,我们可以:
public class Venue {
private String name;
private int capacity;
private ? <WeekDay> list; //structure to hold days, i don't know which one to use yet
/** several methods like **/
numOfSessionsOn();
getSessionsOn();
addSessionOn();
removeSessionOn();
...
}
以下是我的问题:
Session
课程嵌套在enum
吗?enum
可以接受arraylists
吗?有人告诉我,我将在同一天通过所有场地,例如星期一是所有场地的星期一,每个场地都会更新其名单。所以即使没有人发表评论,我认为这是讨论的结束。
答案 0 :(得分:2)
1)是的,你可以。
2)枚举可以拥有普通类所具有的一切。唯一的区别是它有固定数量的对象(例如,你的周日有星期一,星期二等)。所以,你可以有像
这样的东西public enum WeekDay {
MONDAY, ..., FRIDAY;
private final List<Session> sessions;
private WeekDay {
this.sessions = new ArrayList<Session>();
}
}
3)它总是取决于案例。您可以使用EnumSet保存WeekDay
4)您正在尝试将您的Enum用作课程。 Enum应该始终是Atomic(没有状态)。我会让WeekDay成为一个简单的枚举,并将构建类似于我发布的结构:
public enum WeekDay {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
}
// Session have a topic and a duration
public class Session {
private final String topic;
private final Duration duration;
public Session(String title, Duration duration) {
...
}
// getter/setters
}
/**
/* Venue is nothing more than an specialization of Session. So for example
/* A venue is a session of math hat starts at 3pm.
/*/
public class Venue {
private Session session;
private Time startTime;
...
}
/**
* Class that manages the venues that happens in a WeekDay. It can control
* that will not add two Venus that happens at same period of time.
*/
public class WeekDaySchedule {
private WeekDay weekDay;
private Set<Venue> venues;
...
}
/**
* Controls the whole week.
*/
public class WeekSchedule {
private Collection<WeekDaySchedule> weekDaySchedules;
...
}
答案 1 :(得分:1)
如果你在工作日枚举中添加太多内容,你应该考虑一个超类或接口Weekday
并在他自己的班级中实现每个工作日。
如果你真的想在你的枚举中这样做,你可以用这种方式来做:
1)实现方法,在每个方法(和构造函数)中进行切换,如:
switch (this) {
case MONDAY:
return ...
break;
default:
break;
}
2)使方法成为抽象,为每个工作日添加一个匿名实现:
public enum weekday {
MONDAY {
@Override
public Object getSomething() {
// TODO Auto-generated method stub
return null;
}
};
public abstract Object getSomething();
}
3)添加构造函数的参数:
public enum weekday {
MONDAY(new Object());
final private Object object;
private weekday(Object object) {
this.object = object;
}
}
但正如我所说的那样,尽量避免使你的枚举不仅仅是枚举。