在Grails上动态更改src/java文件中的枚举值
我在Grails项目中的src/java上有一个enum文件 此枚举在代码中定义了它的值,我可以动态更改此值吗在Grails上动态更改src/java文件中的枚举值,grails,Grails,我在Grails项目中的src/java上有一个enum文件 此枚举在代码中定义了它的值,我可以动态更改此值吗 public enum Status { value_one, value_two, value_three; public String getOtherString() { switch (this) { case value_one: return "value one";
public enum Status
{
value_one, value_two, value_three;
public String getOtherString()
{
switch (this)
{
case value_one:
return "value one";
case value_two:
return "value two";
case value_three:
return "value three";
default:
return "problem";
}
}
@Override
public String toString()
{
switch (this)
{
case value_one:
return "VALUE 1";
case value_two:
return "VALUE 2";
case value_three:
return "VALUE 3";
default:
return "problem happens";
}
}
}
您可以在使用前使用扩展。例如,通过添加到BootStrap.init()
:
我找到了一种使用服务的方法 我将枚举转换为.groovy文件,并使用服务方法返回值
一切都好 你到底想改变什么?是否添加/删除新枚举?更改枚举名称?更改toString()的响应?更改getOtherString()的响应?我想更改数据库中的字符串响应,例如:
case-value\u-one:return MyDomain.getValueOne()代码>通过这种方式,我有一个名为MyDomain的域,它有一些可以在数据库上更改的属性。
Status.metaClass.getValueOne = {
Status val = delegate
switch (val) {
case Status.value_one:
// read from database
}
}