括号内的值的枚举 [英] Enums with values inside brackets
本文介绍了括号内的值的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨...
我在 http:// docs中看到过一些例子.oracle.com / [ ^ ]包含带有一些变量的枚举在每个enum对象里面,如下面的例子...
Hi...
Ive seen some examples in http://docs.oracle.com/[^] that contains enums with some variables inside each enum objects like the example below...
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
我试图创建一个类似的枚举..
And Ive tried to create an enum like the same..
public class EnumsWithValueInsideBracketsTest
{
public static void main(String[] args)
{
}
enum Days
{
Monday(false), Tuesday(false), Wednesday(false), Thursday(false), Friday(false), Saturday(true), Sunday(true)
}
}
但程序没有运行,它说
But the program doesnt run and it says
constructor Days in enum Days cannot be applied to given types;
required: no argument
found: boolean
reason: actual and formal arguments lists differ in length"
这究竟是什么意思?
如何用布尔值创建一些枚举?
What does that really mean ?
And how can I create some enums with booleans values ?
推荐答案
这意味着你有没有在你的枚举
中创建一个带有布尔值的构造函数。你需要改变它看起来像:
It means that you have not created a constructor in yourenum
which takes a boolean value. You need to change it so it looks like:
public enum Day
{
Monday(false),
Tuesday(false),
Wednesday(false),
Thursday(false),
Friday(false),
Saturday(true),
Sunday(true);
private final boolean weekEnd;
Day(boolean weekEnd) {
this.weekEnd = weekEnd;
}
bool isWeekend()
{
return weekEnd;
}
}
固定类型为布尔
[/ edit]
[edit]
fixed type to boolean
[/edit]
这篇关于括号内的值的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文