3

我已经声明了枚举函数如下

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.

所以请协助我宣布这个枚举。提前致谢。

4

5 回答 5

6

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

  1. 枚举声明中的 static 没有任何意义并导致编译错误。
  2. 您需要在类的开头定义枚举的值(见下文)。
  3. 数组声明应该在静态块之外

这应该可行,我还清理了一些代码:

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()

于 2013-07-23T06:35:22.437 回答
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 


}
于 2013-07-23T06:31:24.473 回答
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类声明不能是静态的。你应该像塞尔吉奥提到的那样做

于 2013-07-23T06:36:37.710 回答
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....
}

您需要的解决方案可以是这个或类似的。

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

例子

&对于带有枚举的静态块

于 2013-07-23T07:06:12.300 回答
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
    }
}
于 2013-07-23T06:32:16.527 回答