Java枚举给出错误?

编程入门 行业动态 更新时间:2024-10-22 17:25:40
本文介绍了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枚举给出错误?

本文发布于:2023-10-28 01:14:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!