本文介绍了Java:在使用整型常量声明枚举时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Urg,我对Java中的枚举如何工作感到困惑。在C#和C ++(我正常使用的东西)中,这似乎还可以,但Java想要让我生气。>

 枚举方向
{
NORTH_WEST = 0x0C,
NORTH = 0x10,
NORTH_EAST = 0x14,
WEST = 0x18,
NONE = 0x20,
EAST = 0x28,
SOUTH_WEST = 0x24,
SOUTH = 0x30,
SOUTH_EAST = 0x3C
}

有人可以告诉我我做错了什么吗?
谢谢



以下是错误:

  --- jGRASP exec:javac -g Test.java 

Test.java:79:',','}'或';'预计
NORTH_WEST = 0x0C,
^
Test.java:79:'}'expected
NORTH_WEST = 0x0C,
^
Test.java:80:< identifier>预期
NORTH = 0x10,
^
Test.java:87:';'expected
SOUTH_EAST = 0x3C
^


解决方案

对于这种情况,您可以简单地使用实例字段

  public enum Direction {
NORTH(0x10),WEST(0x18),...;

private final int code;
Direction(int code){this.code = code; }
public int getCode(){return code;
}

Java 枚举被实现为对象。他们可以有领域和方法。您还可以选择声明构造函数,该构造函数接受一些参数,并在常量声明中为这些参数提供值。



另请参见




  • - 一个快速但全面的指南Java 枚举






附录: EnumSet EnumMap



请注意,这些值可能比实例字段更好。也就是说,如果您尝试设置位字段的值,则应该使用。



两个常量(例如C ++)与结合使用,作为一个紧凑型

  //beforeimplementation,with bitwise operations 

public static final int BUTTON_A = 0x01;
public static final int BUTTON_B = 0x02;
public static final int BUTTON_X = 0x04;
public static final int BUTTON_Y = 0x08;

int buttonState = BUTTON_A | BUTTON_X; // A& X被按下!

if((buttonState& BUTTON_B)!= 0)... // B被按下...

使用枚举 EnumSet ,可以看起来像这样:枚举和枚举

枚举按钮{A,B,X, Ÿ; }

设置< Button> buttonState = EnumSet.of(Button.A,Button.X); // A& X被按下!

if(buttonState.contains(Button.B))... // B被按下...

还有。这是一个,其关键是枚举常量。



所以,如下所示:

  //before,使用int常量和数组索引

public static final int JANUARY = 0; ...

员工[] employeeOfTheMonth = ...

employeeOfTheMonth [JANUARY] = jamesBond;

现在您可以:

  //after,带有枚举和枚举地图

枚举月份{JANUARY,...}

地图< Month,Employee> employeeOfTheMonth = ...

employeeOfTheMonth.put(Month.JANUARY,jamesBond);

在Java中,枚举是一个非常强大的抽象也适用于Java Collections Framework。



另请参见





  • 有效的Java第2版


    • 项目30:使用枚举项目31:使用实例字段而不是ordinals

    • 项目32: int 常量

    • EnumSet 而不是位字段
    • 项目33:使用而不是顺序索引




相关问题




    - 例如 EnumSet EnumMap usage

      Urgh, I'm kind of confused on how enums work in Java. In C# and C++ (what I use normally), this seems okay, but Java wants to get mad at me >.>

         enum Direction
         {
            NORTH_WEST = 0x0C,
            NORTH      = 0x10,
            NORTH_EAST = 0x14,
            WEST       = 0x18,
            NONE       = 0x20,
            EAST       = 0x28,
            SOUTH_WEST = 0x24,
            SOUTH      = 0x30,
            SOUTH_EAST = 0x3C
         }
      

      Could someone tell me what I'm doing wrong?Thanks

      Here are the errors:

       ----jGRASP exec: javac -g Test.java
      
      Test.java:79: ',', '}', or ';' expected
            NORTH_WEST = 0x0C,
                       ^
      Test.java:79: '}' expected
            NORTH_WEST = 0x0C,
                        ^
      Test.java:80: <identifier> expected
            NORTH      = 0x10,
                 ^
      Test.java:87: ';' expected
            SOUTH_EAST = 0x3C
                             ^
      
      解决方案

      For this scenario, it looks like you can simply use an instance field.

      public enum Direction {
         NORTH(0x10), WEST(0x18), ...;
      
         private final int code;
         Direction(int code)  { this.code = code; }
         public int getCode() { return code; }
      }
      

      Java enum are implemented as objects. They can have fields and methods. You also have the option of declaring a constructor that takes some arguments, and providing values for those arguments in your constant declaration. You can use these values to initialize any declared fields.

      See also


      Appendix: EnumSet and EnumMap

      Note that depending on what these values are, you may have an even better option than instance fields. That is, if you're trying to set up values for bit fields, you should just use an EnumSet instead.

      It is common to see powers of two constants in, say, C++, to be used in conjunction with bitwise operations as a compact representation of a set.

      // "before" implementation, with bitwise operations
      
      public static final int BUTTON_A = 0x01;
      public static final int BUTTON_B = 0x02;
      public static final int BUTTON_X = 0x04;
      public static final int BUTTON_Y = 0x08;
      
      int buttonState = BUTTON_A | BUTTON_X; // A & X are pressed!
      
      if ((buttonState & BUTTON_B) != 0) ...   // B is pressed...
      

      With enum and EnumSet, this can look something like this:

      // "after" implementation, with enum and EnumSet
      
      enum Button { A, B, X, Y; }
      
      Set<Button> buttonState = EnumSet.of(Button.A, Button.X); // A & X are pressed!
      
      if (buttonState.contains(Button.B)) ... // B is pressed...
      

      There is also EnumMap that you may want to use. It's a Map whose keys are enum constants.

      So, where as before you may have something like this:

      // "before", with int constants and array indexing
      
      public static final int JANUARY = 0; ...
      
      Employee[] employeeOfTheMonth = ...
      
      employeeOfTheMonth[JANUARY] = jamesBond;
      

      Now you can have:

      // "after", with enum and EnumMap
      
      enum Month { JANUARY, ... }
      
      Map<Month, Employee> employeeOfTheMonth = ...
      
      employeeOfTheMonth.put(Month.JANUARY, jamesBond);
      

      In Java, enum is a very powerful abstraction which also works well with the Java Collections Framework.

      See also

      Related questions

      这篇关于Java:在使用整型常量声明枚举时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:44