Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 枚举引发构造函数错误_Java_Enums - Fatal编程技术网

Java 枚举引发构造函数错误

Java 枚举引发构造函数错误,java,enums,Java,Enums,我创建了一个类来保存货币类型。 类称为Currency。其内容包括— public class Currency{ public enum CurrencyType {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)}; } 在Eclipse中,PENNY、NICKLE等以红色突出显示,错误消息显示- The constructor Currency.CurrencyType(int) is undefined 为什么会出现此错误?

我创建了一个类来保存货币类型。 类称为Currency。其内容包括—

public class Currency{
            public enum CurrencyType {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
}
在Eclipse中,PENNY、NICKLE等以红色突出显示,错误消息显示-

The constructor Currency.CurrencyType(int) is undefined

为什么会出现此错误?

因为没有构造函数
CurrencyType(int)
。如果不将枚举列为PENNY、NICKLE等,则需要创建显式构造函数。

将构造函数添加到枚举中,如下所示:

public enum CurrencyType {
    PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
    int value;
    CurrencyType(int value) {
       this.value = value;
    }
    int getValue() {
       return value;
    }
}

枚举中没有参数(int)的构造函数

可以在枚举中创建构造函数

CurrencyType (int value){//Whatever you want to do with it }

对于未来:错误信息清楚地说明了什么是错的。大多数情况下,只要你读一下错误信息,你就可以省下很多工作。(或者做一点谷歌研究)

您还没有定义接受
CurrencyType
的int的私有构造函数。