如何使用枚举? [英] How to use enum?
问题描述
我写一个code为应用。但我得到以下错误。
I am writing a code for an app. But I am getting the following error.
我张贴的屏幕截图。请帮我解决错误。
I am posting the screen shot. Please help me to resolve the error.
该错误是在我使用的枚举的部分。
The error is in the part where I use enum.
public static enum Mode
{
static
{
CORRECT = new Mode("CORRECT", 2);
Mode[] arrayOfMode = new Mode[3];
arrayOfMode[0] = NO_ASYNC_TASK;
arrayOfMode[1] = NO_DOWNLOADED_DRAWABLE;
arrayOfMode[2] = CORRECT;
}
}
感谢每个人的答案。
Thanks every one for the answers.
推荐答案
您定义需要一些改进。首先,没有必要定义枚举为静态。此外,您还需要与其他班级把所有的枚举在特定的独立的阶级没有,这将使您的解决方案更有条理。此外,静态是保留关键字的语言,所以没有必要使用静态的内部块。
Your definition needs some enhancements. First of all there is no need to define the enumeration as static. Also, you need to put all of your enumerations in a specific independent class not with other classes, this would make your solution more organised. Additionally, 'static' is a reserved keyword for the language, so no need to use static for an inner block.
您可以定义使用或不整数值的枚举作为如下:
You can define the enumerations with or without integer values as in the followings:
public enum Mode
{
NO_ASYNC_TASK,
NO_DOWNLOADED_DRAWABLE,
CORRECT
}
或
public enum Mode
{
NO_ASYNC_TASK(1),
NO_DOWNLOADED_DRAWABLE(2),
CORRECT(3);
private final int id;
Mode(int id) { this.id = id; }
public int getValue() { return id; }
}
这篇关于如何使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!