各位大虾,想问一个有关的java enum 问题
在.net 中 enum元素可以赋值如:
enum Direction { None = 0xff, Up = 2, Down = 4, Left = 8, Right = 16 };
但java里是默认元素为0 1 2 3,在java怎么样才能达到以上效果。如有一段.net 代码:
......
enum Direction { None = 0xff, Up = 2, Down = 4, Left = 8, Right = 16 };
...
 switch (i[x, y])
                {
                    case (int)Direction.Left:
                        x--;
                        break;
                    case (int)Direction.Right:
                        x++;
                        break;
                    case (int)Direction.Up:
                        y--;
                        break;
                    case (int)Direction.Down:
                        y++;
                        break;
                    case (int)Direction.None:
                        return;
                 }
这段代码在java里怎么实现,在网上找了很久也没答案,在这里提问出来,希望能有答案,谢谢各位。

解决方案 »

  1.   

    不明白,既然用了enum,为什么还要要用数字来判断呢?
    一定要这样的话,那还是老办法static final吧。
      

  2.   


    public enum Direction {
    None(0xff), Up(2), Down (4), Left( 8), Right (16);

    private final int value;
    /**
     * Constructor.
     */
    private Direction(int value) {
    this.value = value;
    }

    /**
     * Get the value.
     * @return the value
     */
    public int getValue() {
    return value;
    }

    public static void main(String args[]) throws Exception {
    Direction[] arr = {None, Up, Down, Left, Right};
    for (int i = 0; i < arr.length; i++) {
    switch(arr[i]) {
    case None: System.out.println("None");
    case Up: System.out.println("Up");
    case Down: System.out.println("Down");
    case Left: System.out.println("Left");
    case Right: System.out.println("Right");
    default: System.out.println("Nothing to match");
    }
    }
    }
    }
      

  3.   

    因为i数组存的是数字,java里的enum不能实现吗
      

  4.   

    .NET 和 Java 枚举的实现原理不太一样,如果拿 Java 写上面代码,可以按下面方式写:public enum Direction {UP, DOWN, LEFT, RIGHT}  //Java 中习惯于把枚举常量写成全大写字符的形式Direction[][] i = new Direction[100][100];
    ...
    if (i[x][y] != null) {  //Java 中枚举类型的变量可以取值为 null,所以可以用 null 表示 None 即无方向
        switch (i[x][y]) {
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        }
    }
      

  5.   

    不要为了用enum而用enum,既然你要判断是int,那就用int,没必要非用enum不可。
    如果你一定要用enum,那就把那个数组声明为enum类型。
    是这个道理不?没必要简单问题复杂化。
      

  6.   

    这里说明一下,i数组存的是很多数字,是模仿迷宫存放数字用的,当然可以不用enum,swith里可以数字,但这样可读性是不是比较差,因为enum里存的是方向,如果用数字判断,别人看了就不知这些数字是什么来的
      

  7.   

    4楼的大哥,case LEFT://这里的LEFT可以转为数字吗,因为i数组存的是数字,不是很明白java的enum
      

  8.   

    又要用数字,又要有可读性也可以呀:
    public static final int NONE = 0xff, UP = 2, DOWN = 4, LEFT = 8, RIGHT = 16
    Java没有进入enum之前这种用法遍地都是。
    而且我觉得你的情况本来就不适合用enum。
      

  9.   

    我说一点:case标签的参数可以是整型与枚举常量。
    enum Direction {
        None(0xff), Up(2), Down (4), Left( 8), Right (16);
        
        private final int value;
        /**
         * Constructor.
         */
        private Direction(int value) {
            this.value = value;
        }
        
        /**
         * Get the value.
         * @return the value
         */
        public int getValue() {
            return value;
        }
    public class T{
        public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String input = in.next();
    Direction dir = Enum.valueOf(Direction.class, input);
    switch (dir) {
    case None:
    System.out.println("None");
    break;
    case Up:
    System.out.println("Up");
    break;
    }
    }}还有一点要说的是,虽然说,JAVA默认的0,1,2,3,但是可以改变,详见JDK1.6