返回相反方向的枚举

我想声明一个枚举方向,它有一个返回相反方向的方法(下面的句法不正确,例如,枚举不能被实例化,但它说明了我的观点)。这在 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);
}
}
163358 次浏览

是的,我们一直这样做。您返回一个静态实例,而不是一个新的对象

 static Direction getOppositeDirection(Direction d){
Direction result = null;
if (d != null){
int newCode = -d.getCode();
for (Direction direction : Direction.values()){
if (d.getCode() == newCode){
result = direction;
}
}
}
return result;
}

对于那些被标题吸引到这里的人: 是的,你可以在你的枚举中定义你自己的方法。如果您想知道如何调用这样的非静态方法,您可以采用与任何其他非静态方法相同的方法——在定义或继承该方法的类型的实例上调用它。对于枚举,这些实例只是 ENUM_CONSTANT

所以你只需要 EnumType.ENUM_CONSTANT.methodName(arguments)


现在让我们回到问题从问题。解决方案之一可能是

public enum Direction {


NORTH, SOUTH, EAST, WEST;


private Direction opposite;


static {
NORTH.opposite = SOUTH;
SOUTH.opposite = NORTH;
EAST.opposite = WEST;
WEST.opposite = EAST;
}


public Direction getOppositeDirection() {
return opposite;
}


}

现在 Direction.NORTH.getOppositeDirection()将返回 Direction.SOUTH


这里有一些更“古怪”的方法来说明 @ jedward 评论,但是它感觉不像第一种方法那样灵活,因为添加更多的字段或更改它们的顺序会破坏我们的代码。

public enum Direction {
NORTH, EAST, SOUTH, WEST;


// cached values to avoid recreating such array each time method is called
private static final Direction[] VALUES = values();


public Direction getOppositeDirection() {
return VALUES[(ordinal() + 2) % 4];
}
}

创建一个抽象方法,并让每个枚举值重写它。由于在创建时您知道相反的情况,所以不需要动态地生成或创建它。

It doesn't read nicely though; perhaps a switch would be more manageable?

public enum Direction {
NORTH(1) {
@Override
public Direction getOppositeDirection() {
return Direction.SOUTH;
}
},
SOUTH(-1) {
@Override
public Direction getOppositeDirection() {
return Direction.NORTH;
}
},
EAST(-2) {
@Override
public Direction getOppositeDirection() {
return Direction.WEST;
}
},
WEST(2) {
@Override
public Direction getOppositeDirection() {
return Direction.EAST;
}
};


Direction(int code){
this.code=code;
}
protected int code;


public int getCode() {
return this.code;
}


public abstract Direction getOppositeDirection();
}

对于像这样的小枚举,我发现最可读的解决方案是:

public enum Direction {


NORTH {
@Override
public Direction getOppositeDirection() {
return SOUTH;
}
},
SOUTH {
@Override
public Direction getOppositeDirection() {
return NORTH;
}
},
EAST {
@Override
public Direction getOppositeDirection() {
return WEST;
}
},
WEST {
@Override
public Direction getOppositeDirection() {
return EAST;
}
};




public abstract Direction getOppositeDirection();


}

这种方法是有效的:

public enum Direction {
NORTH, SOUTH, EAST, WEST;


public Direction oppose() {
switch(this) {
case NORTH: return SOUTH;
case SOUTH: return NORTH;
case EAST:  return WEST;
case WEST:  return EAST;
}
throw new RuntimeException("Case not implemented");
}
}
public enum Direction {
NORTH, EAST, SOUTH, WEST;


public Direction getOppositeDirection(){
return Direction.values()[(this.ordinal() + 2) % 4];
}
}

枚举具有一个静态值方法,该方法返回一个数组,该数组按声明的顺序包含枚举的所有值。来源

因为北方得到1,东方得到2,南方得到3,西方得到4; 你可以创建一个简单的方程,得到相反的一个:

(价值 + 2)% 4