[英]Simulate a class of type enum
how can I do to simulate a class of type enum in java <5.0 ..?? 我怎么能在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));
}
}
Use the typesafe enum described in effective java. 使用有效java中描述的typesafe枚举。 Here is an example given from Joshua Blochs article on this :
以下是Joshua Blochs关于此问题的文章 :
// The typesafe enum pattern
public class Suit {
private final String name;
private Suit(String name) { this.name = name; }
public String toString() { return name; }
public static final Suit CLUBS =
new Suit("clubs");
public static final Suit DIAMONDS =
new Suit("diamonds");
public static final Suit HEARTS =
new Suit("hearts");
public static final Suit SPADES =
new Suit("spades");
}
If you want your typesafe enum to be Serializable , remember to control reconstruction via the readResolve
method. 如果您希望您的类型安全枚举是可序列化的 ,请记住通过
readResolve
方法控制重建。
Joshua Bloch shows you how in the first edition of his "Effective Java". Joshua Bloch向您展示了他的“Effective Java”的第一版中的情况。 I have to go to work, so I can't type in details now.
我必须去上班,所以我现在不能输入细节。 More to follow.
更多要遵循。
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 convertible int
values and enum
constants. Krock和Riduidel建议的解决方案非常简洁,但AFAIK是一个
switch-case
语句,因为它仅适用于可转换的int
值和enum
常量。 switch-case
combination is a nice feature, more readable than a bunch of if
s (BTW: switch-case
will be compiled as a bunch of if
s anyway, correct?). switch-case
组合是一个很好的功能,比一堆if
更具可读性(BTW: switch-case
将被编译成一堆if
无论如何,对吗?)。 So, to complete the discussed pattern, could someone tell whether there is a quick way to enable this patteren with functionality allowing switch-case
? 因此,为了完成所讨论的模式,有人可以告诉是否有一种快速的方法来启用这个模式,并允许使用
switch-case
功能? Adding an int
property and getter for it seems the simplest way, but is it the optimal? 为它添加一个
int
属性和getter似乎是最简单的方法,但它是最优的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.