反対方向を返すメソッドを持つ enum Direction を宣言したいと思います (以下は構文的に正しくありません。つまり、enum はインスタンス化できませんが、私の要点を示しています)。これはJavaで可能ですか?
コードは次のとおりです。
public enum Direction {
NORTH(1),
SOUTH(-1),
EAST(-2),
WEST(2);
Direction(int code){
this.code=code;
}
protected int code;
public int getCode() {
return this.code;
}
static Direction getOppositeDirection(Direction d){
return new Direction(d.getCode() * -1);
}
}