Java 枚举方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18883646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Enum Methods
提问by Skyler
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?
我想声明一个枚举方向,它有一个返回相反方向的方法(以下在语法上不正确,即无法实例化枚举,但它说明了我的观点)。这在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);
}
}
采纳答案by Pshemo
For those lured here by title: yes, you can define your own methods in your enum. If you are wondering how to invoke such non-static method, you do it same way as with any other non-static method - you invoke it on instance of type which defines or inherits that method. In case of enums such instances are simply ENUM_CONSTANT
s.
对于那些被标题吸引到这里的人:是的,您可以在枚举中定义自己的方法。如果您想知道如何调用这样的非静态方法,您可以按照与任何其他非静态方法相同的方式进行调用 - 在定义或继承该方法的类型实例上调用它。在枚举的情况下,此类实例只是ENUM_CONSTANT
s。
So all you need is EnumType.ENUM_CONSTANT.methodName(arguments)
.
所以你只需要EnumType.ENUM_CONSTANT.methodName(arguments)
.
Now lets go back to problem from question. One of solutions could be
现在让我们从问题回到问题。解决方案之一可能是
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;
}
}
Now Direction.NORTH.getOppositeDirection()
will return Direction.SOUTH
.
现在Direction.NORTH.getOppositeDirection()
将返回Direction.SOUTH
。
Here is little more "hacky" way to illustrate @jedwards commentbut it doesn't feel as flexible as first approach since adding more fields or changing their order will break our code.
这里有一些更“hacky”的方式来说明@jedwards 评论,但感觉不如第一种方法灵活,因为添加更多字段或更改它们的顺序会破坏我们的代码。
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];
}
}
回答by BevynQ
Yes we do it all the time. You return a static instance rather than a new Object
是的,我们一直都在这样做。你返回一个静态实例而不是一个新的对象
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;
}
回答by Makoto
Create an abstract method, and have each of your enumeration values override it. Since you know the opposite while you're creating it, there's no need to dynamically generate or create it.
创建一个抽象方法,并让每个枚举值覆盖它。由于您在创建它时知道相反的情况,因此无需动态生成或创建它。
It doesn't read nicely though; perhaps a switch
would be more manageable?
不过它读起来不太好;也许 aswitch
会更易于管理?
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();
}
回答by Amir Afghani
For a small enum like this, I find the most readable solution to be:
对于像这样的小枚举,我发现最易读的解决方案是:
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();
}
回答by Pikrass
This works:
这有效:
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");
}
}
回答by Pregunton
public enum Direction {
NORTH, EAST, SOUTH, WEST;
public Direction getOppositeDirection(){
return Direction.values()[(this.ordinal() + 2) % 4];
}
}
Enums have a static values method that returns an array containing all of the values of the enum in the order they are declared. source
枚举有一个静态值方法,该方法返回一个数组,该数组包含按声明顺序排列的枚举的所有值。来源
since NORTH gets 1, EAST gets 2, SOUTH gets 3, WEST gets 4; you can create a simple equation to get the opposite one:
因为北得 1,东得 2,南得 3,西得 4;你可以创建一个简单的方程来得到相反的方程:
(value + 2) % 4
(值 + 2) % 4