java 如何在java中创建枚举对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28436543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make objects of an enum in java
提问by tb1000
I am trying to define three different objects of enum class "Item" with weights between 0 and 20 and description of each item
我正在尝试定义三个不同的枚举类“Item”对象,其权重在 0 到 20 之间以及每个项目的描述
Here is my code :
这是我的代码:
public enum Item
{
// Three Items with descriptions
GOLD (2, "gold"), SILVER(12, "silver"), BRONZE(5, "bronze");
private int weight;
private String description;
public int getWeight()
{
return weight;
}
public String toString()
{
return description;
}
}
I keep getting this error
我不断收到此错误
constructor Item in enum Item cannot be applied to given types: required: no arguments, found:int.java.lang.String, reason: actual and formal argument lists differ in length
枚举项中的构造函数项不能应用于给定类型:必需:无参数,找到:int.java.lang.String,原因:实际和形式参数列表的长度不同
Also
还
The operator that you use here cannot be used for the type of value that you are using it for. You either using the wrong type here, or the wrong operator
您在此处使用的运算符不能用于您使用它的值类型。您要么在这里使用了错误的类型,要么使用了错误的运算符
回答by NiziL
You forgot to declare a constructor !
你忘了声明一个构造函数!
public enum Item {
GOLD(2, "gold"), SILVER(12, "silver"), BRONZE(5, "bronze");
private int weight;
private String description;
private Item(int weight, String description) {
this.weight = weight ;
this.description = description ;
}
// rest of code
}
Long story short
In your code GOLD(2, "gold")
can be read as GOLD = new Item(2, "gold")
, so the compiler doesn't find the appropriate constructor.
长话短说
在您的代码GOLD(2, "gold")
中可以读作GOLD = new Item(2, "gold")
,因此编译器找不到合适的构造函数。
More complete answer and examples
Don't forget that everything (well, almost everything, there are some primitive like int
or boolean
) is an object in Java. And so, items of your enum
are also objects, and need to be instantiate with a constructor.
Keep in mind that there is an implicit parameterless constructor if you don't provide an explicit constructor.
更完整的答案和示例
不要忘记,Java中的所有东西(好吧,几乎所有东西,都有一些原始的像int
or boolean
)都是一个对象。因此,您的项目enum
也是对象,需要使用构造函数进行实例化。
请记住,如果您不提供显式构造函数,则存在隐式无参数构造函数。
public enum Test {
A, B, C;
}
public enum Test2 {
A, B, C;
private int value ;
private Test2(){
value = 0;
}
}
public enum Test3 {
A(1), B(2), C(3);
}
public enum Test4 {
A, B, C;
public Test4(int value) {}
}
Test
and Test2
are valid, because there is a parameterless constructor (implicit for Test
). But Test3
and Test4
try to instantiate A
, B
and C
with a constructor which doesn't exist.
Test
并且Test2
是有效的,因为有一个无参数构造函数(隐式为Test
)。但是Test3
并Test4
尝试实例化A
,B
并C
使用不存在的构造函数。
回答by DennisW
You need to define an appropriate constructor:
您需要定义一个合适的构造函数:
private Item(int weight, String description) {
this.weight = weight;
this.description = description;
}