Java枚举方法 [英] Java Enum Methods

查看:127
本文介绍了Java枚举方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明一个枚举方向,它具有返回相反方向的方法(以下不是语法上正确的,即枚举不能被实例化,但是它说明了我的观点)。这是可能的Java?



这是代码:

  public enum Direction {

NORTH(1),
SOUTH(-1),
EAST(-2),
WEST(2);

方向(int code){
this.code = code;
}
protected int code;
public int getCode(){
return this.code;
}
static方向getOppositeDirection(Direction d){
return new Direction(d.getCode()* -1);
}
}


解决方案

那些被标题引诱的人:是的,你可以在枚举中定义自己的方法。如果您想知道如何调用这样的非静态方法,则可以像其他任何非静态方法一样进行调用 - 您可以在定义或继承该方法的类型的实例上调用它。在枚举的情况下,这些实例只是简单的 ENUM_CONSTANT s。



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






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

  public enum Direction {

NORTH,SOUTH,EAST,WEST;

私人方向对面;

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

public Direction getOppositeDirection(){
return opposite;
}

}

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






以下是hacky的一种方式来说明 @jedwards评论但是它不像第一种方法那么灵活

  public enum Direction {
NORTH,东,西,西;

private static final Direction [] cachedValues = values(); //避免重新创建数组

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


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);
     }
}

解决方案

For those lured here by title: yes, you can define your own methods in 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_CONSTANTs.

So all you need is 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.


Here is little more "hacky" way to illustrate @jedwards comment but it is doesn't feel as flexible as first approach

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

    private static final Direction[] cachedValues = values();// to avoid recreating array

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

这篇关于Java枚举方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆