java枚举类型是jdk5出现的.它的出现主要为了解决一些有特殊意义,已经确定的,长度不会改变的集合.

下面代码,创建一个月份的描述类,并且给出确定的12个月份.

//月份描述
public class Month {
    //月份名称
    private final String name;
    //月份天数
    private final int days;

    //构造子,给出一个月份名称,默认天数31天
    private Month(String name) {
        this(name, 31);
    }
    //构造子,给出月份名称,月份天数
    private Month(String name, int days) {
        this.name = name;
        this.days = days;
    }

    /*
    * 创建12个月份
    * */
    public static final Month JAN = new Month("January");
    public static final Month FEB = new Month("February", 28);
    public static final Month MAR = new Month("March");
    public static final Month APR = new Month("April", 30);
    public static final Month MAY = new Month("May");
    public static final Month JUN = new Month("June", 30);
    public static final Month JUL = new Month("July");
    public static final Month AUG = new Month("August");
    public static final Month SEP = new Month("September", 30);
    public static final Month OCT = new Month("October");
    public static final Month NOV = new Month("November", 30);
    public static final Month DEC = new Month("December");

    //获取月份名称
    public String getName() {
        return name;
    }

    //获取月份值
    public int getDays() {
        return days;
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(Month.JAN);//demo7.Month@1b6d3586
        System.out.println(Month.JAN.getName());//January
        System.out.println(Month.JAN.getDays());//31
    }
}
02-13 11:25