用String数组声明java枚举 [英] Declare java enum with a String array
问题描述
我正在根据从数据库检索的数据来声明一个枚举类型。我有一个方法返回表中所有行的字符串数组,我想使其成为枚举类型。有没有办法用数组来构造一个枚举?
I'm trying to declare an enum type based on data that I'm retrieving from a database. I have a method that returns a string array of all the rows in the table that I want to make into an enumerated type. Is there any way to construct an enum with an array?
这是我试过的,但是从看到eclipse的方式看来,这只是创建了一个方法通过该名称:
This is what I tried, but from the way it looked in eclipse, it seemed like this just created a method by that name:
public enum ConditionCodes{
Condition.getDescriptions();
}
提前谢谢!
推荐答案
你不能。
枚举的值
在编译时必须知道 。如果你还有其他的东西,那么它不是一个枚举
。
The values of an enum
must be known at compile time. If you have anything else, then it's not an enum
.
你可以通过类似的实现在Java语言介绍支持之前使用的旧类型安全枚举这种技术通过枚举
关键字。您可以使用这些技术,但只需使用从DB读取的值替换 static final
字段。
You could come rather close via an implementation that's similar to the old typesafe enums that were used before the Java language introduced support for this technique via the enum
keyword. You could use those techniques but simply replace the static final
fields with values read from the DB.
这篇关于用String数组声明java枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!