初心者無視! Java相談室 その9
■ このスレッドは過去ログ倉庫に格納されています
0772デフォルトの名無しさん
NGNGEffective Java 読め。
まーイディオムって言うと語弊があるかもしれんが…。
public class Direction implements Serializable
{
public static final Direction NORTH = new Direction(0);
public static final Direction SOUTH = new Direction(1);
public static final Direction EAST = new Direction(2);
public static final Direction WEST = new Direction(3);
private Direction(int id) {
this.id = id;
}
private static final Direction[] value_enum
= { NORTH, SOUTH, EAST, WEST };
private Object readResolve() throws ObjectStreamException {
if (0 <= this.id && this.id < value_enum.length) {
return value_enum[this.id];
} else {
throw new ObjectStreamException();
}
}
private int id;
}
■ このスレッドは過去ログ倉庫に格納されています