如何在grails中使用Enum(不在域类中)

bsr*_*bsr 7 grails groovy enums

我想使用Enum来表示一些选择值.在/src/groovy文件夹中,在包下面com.test,我有这个枚举:

package com.test

public  enum TabSelectorEnum {
  A(1), B(2)

  private final int value
  public int value() {return value}

}
Run Code Online (Sandbox Code Playgroud)

现在,我试图从控制器访问它,如:

TabSelectorEnum.B.value()
Run Code Online (Sandbox Code Playgroud)

但它引发了一个例外:

Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: Could not initialize class com.test.TabSelectorEnum
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?


更新:清理并重新编译后,错误代码更改为:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.test.TabSelectorEnum(java.lang.String, java.lang.Integer, java.lang.Integer)
Run Code Online (Sandbox Code Playgroud)

在访问Enum的价值方面似乎有些不对劲,但我不知道是什么.

Bur*_*ith 15

您没有为int值定义构造函数:

package com.test

enum TabSelectorEnum {
   A(1),
   B(2)

   private final int value

   private TabSelectorEnum(int value) {
      this.value = value
   }

   int value() { value }
}
Run Code Online (Sandbox Code Playgroud)