Java枚举给出错误? [英] Java enum giving an error?

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

问题描述



枚举状态下的构造函数状态不能应用于给定的类型; STATUS_OPEN(0),



为什么会发生这种情况,我该如何解决?



这是我的代码到目前为止:

  public enum Status 
{
STATUS_OPEN(0),
STATUS_STARTED(1),
STATUS_INPROGRESS(2),
STATUS_ONHOLD(3),
STATUS_COMPLETED(4),
STATUS_CLOSED(5);

}

我正在使用记事本和jdk通过命令提示符 - 我现在不想使用netbeans或eclipse。另外,我从来没有使用过enum或java!所以请放开我..



哦,是的,我在跟随这个网站:链接



我有googled around,我找不到为什么这个问题发生或如何解决它通过搜索错误...

解决方案

p>您需要在枚举中添加一个构造函数。

  public enum Status {
STATUS_OPEN(0),
STATUS_STARTED(1),
STATUS_INPROGRESS(2),
STATUS_ONHOLD(3),
STATUS_COMPLETED(4),
STATUS_CLOSED(5);

private final int number;
状态(int number){
this.number = number;
}

public int getMagicNumber(){return number; }
}

这将修复你的语法问题,但是你希望达到什么与号码?通常使用枚举,而不是数字的需要。


When i try to give a value to my enum, it gives me this error:

constructor status in enum status cannot be applied to given types; STATUS_OPEN(0),

why is this happening and how do i fix it?

here is my code thus far:

 public enum Status 
 { 
     STATUS_OPEN(0),  
     STATUS_STARTED(1),  
     STATUS_INPROGRESS(2),  
     STATUS_ONHOLD(3),  
     STATUS_COMPLETED(4),  
     STATUS_CLOSED(5);  

 }

i'm using notepad and the jdk via command prompt - i don't want to use netbeans or eclipse at the moment. Also, i've never used an enum before or java! So please bare with me..

Oh yes, and i was following this site: link

I've googled around and I couldn't really find why this issue is occuring or how to fix it by searching for the error...

解决方案

You need to add a constructor to the enum.

public enum Status {
   STATUS_OPEN(0),  
   STATUS_STARTED(1),  
   STATUS_INPROGRESS(2),  
   STATUS_ONHOLD(3),  
   STATUS_COMPLETED(4),
   STATUS_CLOSED(5);

   private final int number;
   Status(int number) { 
       this.number = number;
   }

   public int getMagicNumber() { return number; } 
}

This'll fix your syntax problems, but what are you hoping to achieve with the number? Enums are often used instead of the need for numbers at all.

这篇关于Java枚举给出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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