Java Enum Methods
I would like to declare an enum Direction, that has a method that returns
the opposite direction (the following is not syntactically correct, i.e,
enums cannot be instantiated, but it illustrates my point). Is this
possible in Java?
Here is the code:
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);
}
}
No comments:
Post a Comment