[...] 所有的值构造函数都是空的(不带参数,即字段),我们可以将其作为 Enum 类型类的一部分。
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum)
现在,如果我需要几个月,那将是
data month = January | February | March | April | May | June | July | August |September | October | November |December deriving (Eq, Ord, Show, Read, Bounded, Enum)
我的问题是:
- 每个月在哪里存储 Max Days 值?
- 如果是闰年,如何提及和控制二月份的月份,那么二月份的 maxDays 是 29 天,否则它将是 28 天?
在 Java 中,可以编写如下代码:
public enum Month {
January (31),
February (29),
March (31),
April (30),
May (31),
June (30),
July (31),
August (31),
September (30),
October (31),
November (30),
December (31),
;
private int maxDays; // instance variables
private (int maxDays) { // constructors always private
this.maxDays = maxDays;
}
Public int getMaxDays () {
return maxDays;
}