我有以下枚举:
public enum Months {
JAN(31),
FEB(28),
MAR(31),
APR(30),
MAY(31),
JUN(30),
JUL(31),
AUG(31),
SEP(30),
OCT(31),
NOV(30),
DEC(31);
private final byte DAYS; //days in the month
private Months(byte numberOfDays){
this.DAYS = numberOfDays;
}//end constructor
public byte getDays(){
return this.Days;
}//end method getDays
}//end enum Months
尽管我传递了有效的字节参数,但它给了我一个错误,提示“构造函数 Months(int) 未定义”。 我做错了什么?
最佳答案
最简单的解决方案是接受 int
值
private Months(int numberOfDays){
this.DAYS = (byte) numberOfDays;
}
顺便说一句,非静态字段应该采用 camelCase
格式,而不是 UPPER_CASE
另外,有些年份的 2 月有 29 天。
public static boolean isLeapYear(int year) {
// assume Gregorian calendar for all time
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public int getDays(int year) {
return days + (this == FEB && isLeapYear(year) ? 1 : 0);
}
关于java - 为什么字节参数不被识别为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18268597/