如何在grails中使用枚举(不在域类中) [英] How to use Enum in grails (not in domain class)

查看:142
本文介绍了如何在grails中使用枚举(不在域类中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  package com.test 

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

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

}

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

  TabSelectorEnum。 B.value()

但它会引发异常:

 导致:org.codehaus.groovy.runtime.InvokerInvocationException:java.lang.NoClassDefFoundError:无法初始化类com.test.TabSelectorEnum 

我做错了什么?






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

  groovy.lang.GroovyRuntimeException:找不到匹配构造函数:com.test.TabSelectorEnum(java.lang.String,java.lang.Integer,jav a.lang.Integer)

看起来像访问该值的方式有问题枚举,但我不知道什么。

解决方案

你没有为int值定义一个构造函数:

  package com.test 

枚举TabSelectorEnum {
A(1),
B (2)

private final int value

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

int value(){value}
}


I want to use an Enum to represent some selection values. In the /src/groovy folder, under the package com.test, I have this Enum:

package com.test

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

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

}

Now, I am trying to access it from controller like:

TabSelectorEnum.B.value()

but it throws an exception:

Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: Could not initialize class com.test.TabSelectorEnum

What am I doing wrong?


Update: After I cleaned and recompiled, the error code changed to:

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

It seems like there is something wrong in the way accessing the value of the Enum, but I don't know what.

解决方案

You didn't define a constructor for the int value:

package com.test

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

   private final int value

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

   int value() { value }
}

这篇关于如何在grails中使用枚举(不在域类中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆