java中的枚举声明

时间:2013-07-23 06:27:37

标签: java android

我已声明枚举函数如下

public static enum SHAPE


{
    static
    {
      LINE = new SHAPE("LINE", 3);
      CIRCLE = new SHAPE("CIRCLE", 4);
      TEXT = new SHAPE("TEXT", 5);
      SHAPE[] arrayOfSHAPE = new SHAPE[6];
      arrayOfSHAPE[0] = DRAW;
      arrayOfSHAPE[1] = SQUARE;
      arrayOfSHAPE[2] = TRIANGLE;
      arrayOfSHAPE[3] = LINE;
      arrayOfSHAPE[4] = CIRCLE;
      arrayOfSHAPE[5] = TEXT;
    }
  }

但我正在

Syntax error, insert "}" to complete Block at line4 and getting Syntax error, insert "EnumBody" to complete EnumDeclaration at line1.

所以请协助我声明这个枚举。 提前谢谢。

5 个答案:

答案 0 :(得分:6)

您的枚举声明存在多个问题:

  1. 枚举声明中的静态没有任何意义,导致编译错误。
  2. 您需要在课程开始时定义枚举的值(见下文)。
  3. 数组声明应在静态块之外
  4. 这应该可行,我还清理了一点代码:

    public enum Shape {
        LINE("LINE", 3),
        CIRCLE("CIRCLE", 4),
        TEXT("TEXT", 5),
        // DRAW, SQUaRE, TRIANGLE, ...
        ;
    
        public static final Shape[] SHAPES= new Shape[6];
    
        static
        {
          SHAPES[0] = DRAW;
          SHAPES[1] = SQUARE;
          SHAPES[2] = TRIANGLE;
          SHAPES[3] = LINE;
          SHAPES[4] = CIRCLE;
          SHAPES[5] = TEXT;
        }
    
        // Constructor etc.
    }
    

    我认为您不需要SHAPES数组,因为您可以通过调用Shape.values()

    来访问枚举的所有值

答案 1 :(得分:3)

java中的枚举是:

public enum SHAPE {

      LINE("LINE", 3),
      CIRCLE("CIRCLE", 4),
      TEXT("TEXT", 5);

      private String type;
      private Integer id;

      SHAPE(String type,Integer id){
        this.type= type;
        this.id = id;  
      }    

      // Getters for type and id 


}

答案 2 :(得分:1)

首先,我不知道你在尝试做什么。但是根据enum声明,您没有指定任何实例(它为空)。

public enum SHAPE
{
;//define a seperator where one declares instances.
static
{
  SHAPE LINE = new SHAPE("LINE", 3);
  SHAPE CIRCLE = new SHAPE("CIRCLE", 4);
  SHAPE TEXT = new SHAPE("TEXT", 5);
  SHAPE[] arrayOfSHAPE = new SHAPE[6];
  arrayOfSHAPE[0] = DRAW;
  arrayOfSHAPE[1] = SQUARE;
  arrayOfSHAPE[2] = TRIANGLE;
  arrayOfSHAPE[3] = LINE;
  arrayOfSHAPE[4] = CIRCLE;
  arrayOfSHAPE[5] = TEXT;
 }
}

enum类声明不能是静态的。你应该像Sergio提到的那样做

答案 3 :(得分:1)

    public enum SHAPE {


    LINE("LINE", 3),
    CIRCLE("CIRCLE", 4),
    TEXT("TEXT", 5);

    public final String shapeName;
    public final int shapeValue;
    public static final SHAPE[] arrayOfShap = values();

    static {
        // This is used to do some operations on enum generally like creating a map of this enums. 
    }

    SHAPE(String name, int value){
        this.shapeName = name;
        this.shapeValue = value;
    }

    // Here one can define static methods to get enum values from arrays, map etc....
}

您需要的解决方案可以是此类似的。

有关更多参考资料,请查看以下链接...

java example

&安培; For static block with enum

答案 4 :(得分:0)

使用逗号分隔符

public static enum SHAPE
{
    static
    {
      LINE = new SHAPE("LINE", 3),
      CIRCLE = new SHAPE("CIRCLE", 4),
      TEXT = new SHAPE("TEXT", 5),
      SHAPE[] arrayOfSHAPE = new SHAPE[6],
      arrayOfSHAPE[0] = DRAW,
      arrayOfSHAPE[1] = SQUARE,
      arrayOfSHAPE[2] = TRIANGLE,
      arrayOfSHAPE[3] = LINE,
      arrayOfSHAPE[4] = CIRCLE,
      arrayOfSHAPE[5] = TEXT
    }
}