模拟枚举类型的类
我怎样才能在java <5.0 ..中模拟枚举类型的类?
public final class Week {
private static final Day[] _week = {Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};
public static Day getDayOfWeek(final int index) {
if (index >= 1 && index <= 7) {
return _week[index - 1];
}
throw new IndexOutOfBoundsException("Invalid index [1..7]");
}
public static final class Day {
public static final Day MONDAY = new Day(1, "Monday");
public static final Day TUESDAY = new Day(2, "Tuesday");
public static final Day WEDNESDAY = new Day(3, "Wednesday");
public static final Day THURSDAY = new Day(4, "Thursday");
public static final Day FRIDAY = new Day(5, "Friday");
public static final Day SATURDAY = new Day(6, "Saturday");
public static final Day SUNDAY = new Day(7, "Sunday");
private int _value;
private String _name;
private Day(final int value, final String name) {
_value = value;
_name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "[Day] " + _name;
}
/* (non-Javadoc)
* @see java.lang.String#equals(java.lang.Object)
*/
public boolean equals(Object anObject) {
Day d = (Day)anObject;
return _value == d._value;
}
}
public static void main(String[] agrs) {
System.out.println(Week.getDayOfWeek(2));
}
}
how can I do to simulate a class of type enum in java <5.0 ..??
public final class Week {
private static final Day[] _week = {Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};
public static Day getDayOfWeek(final int index) {
if (index >= 1 && index <= 7) {
return _week[index - 1];
}
throw new IndexOutOfBoundsException("Invalid index [1..7]");
}
public static final class Day {
public static final Day MONDAY = new Day(1, "Monday");
public static final Day TUESDAY = new Day(2, "Tuesday");
public static final Day WEDNESDAY = new Day(3, "Wednesday");
public static final Day THURSDAY = new Day(4, "Thursday");
public static final Day FRIDAY = new Day(5, "Friday");
public static final Day SATURDAY = new Day(6, "Saturday");
public static final Day SUNDAY = new Day(7, "Sunday");
private int _value;
private String _name;
private Day(final int value, final String name) {
_value = value;
_name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return "[Day] " + _name;
}
/* (non-Javadoc)
* @see java.lang.String#equals(java.lang.Object)
*/
public boolean equals(Object anObject) {
Day d = (Day)anObject;
return _value == d._value;
}
}
public static void main(String[] agrs) {
System.out.println(Week.getDayOfWeek(2));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用有效的java中描述的类型安全枚举。以下是 Joshua Blochs 文章中给出的示例:
如果您希望类型安全枚举 Serialized< /a>,记住通过 readResolve 方法控制重建。
Use the typesafe enum described in effective java. Here is an example given from Joshua Blochs article on this:
If you want your typesafe enum to be Serializable, remember to control reconstruction via the
readResolve
method.Joshua Bloch 很久以前写了一篇文章对此进行了解释。
Joshua Bloch wrote, long ago, an article explaining it.
Joshua Bloch 在他的《Effective Java》第一版中向您展示了如何操作。我要去上班,所以现在无法输入详细信息。更多内容敬请关注。
Joshua Bloch shows you how in the first edition of his "Effective Java". I have to go to work, so I can't type in details now. More to follow.
Krock 和 Riduidel 建议的解决方案非常简洁,但据我所知,
switch-case
语句是不可能的,因为它仅适用于可转换int
值和enum常量。
switch-case
组合是一个很好的功能,比一堆if
更具可读性(顺便说一句:switch-case
将被编译为一堆无论如何,if
,对吗?)。那么,为了完成所讨论的模式,有人可以告诉我是否有一种快速的方法来启用此模式并具有允许switch-case
的功能?添加int
属性和 getter 似乎是最简单的方法,但它是最佳方法吗?The solution Krock and Riduidel are suggesting is pretty neat, but AFAIK a
switch-case
statement won't be possible as it's applicable only for convertibleint
values andenum
constants.switch-case
combination is a nice feature, more readable than a bunch ofif
s (BTW:switch-case
will be compiled as a bunch ofif
s anyway, correct?). So, to complete the discussed pattern, could someone tell whether there is a quick way to enable this patteren with functionality allowingswitch-case
? Adding anint
property and getter for it seems the simplest way, but is it the optimal?