java enum 报错,Java枚举给出错误?

在尝试为Java枚举赋值时遇到了构造函数错误。问题在于枚举中没有定义接受整数参数的构造函数。解决方法是在枚举中添加一个私有的构造函数,将整数值与枚举常量关联。同时,添加了一个`getMagicNumber`方法以获取关联的整数值。枚举通常用于替代特定数值,提供类型安全的常量集合。
摘要由CSDN通过智能技术生成

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值