Java: Having trouble declaring an enum with integer constants

后端 未结 3 1987
庸人自扰
庸人自扰 2021-01-19 17:19

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 Dire         


        
3条回答
  •  无人共我
    2021-01-19 17:34

    In Java enums don't hold any other values by default. You'll have to create a private field to store one. Try something like this

    enum Direction {
       NORTH_WEST(0x0C),
       NORTH(0x10),
       ...
    
       private final int code;
       private Direction(int code) {
           this.code = code;
       }
    }
    

    Add getter if necessary.

提交回复
热议问题