如何使用枚举? [英] How to use enum?

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

问题描述

我正在为一个应用程序编写代码。但是我收到以下错误。



我发布了屏幕截图。请帮我解决错误。





错误在我使用枚举的部分。

  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;
}
}

感谢每一个答案。

解决方案

您的定义需要一些增强功能。首先,没有必要将枚举定义为静态。此外,您需要将所有枚举都放在特定的独立类中,而不是与其他类一起使用,这将使您的解决方案更有条理。另外,static是语言的保留关键字,因此不需要为内部块使用static。



您可以定义具有或不具有整数值的枚举,如下所示:

  public enum Mode 
{
NO_ASYNC_TASK,
NO_DOWNLOADED_DRAWABLE,
CORRECT
}

 公开枚举模式
{
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; }
}


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
}

or

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屋!

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