枚举

枚举

枚举要注意的细节:

枚举_第1张图片
注意细节

小练习

package com.cyl.meiju;

import java.util.Calendar;

enum Season {
    spring("春天"),summer("夏天"),autumn("秋天"),winter("冬天");
    String value;
    private  Season(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
}

class DateDemo {
    private Season season;
    private int year;
    private int month;
    private int date;
    
    public Season getSeason() {
        return season;
    }
    public void setSeason(Season season) {
        this.season = season;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int i) {
        this.year = i;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDate() {
        return date;
    }
    public void setDate(int i) {
        this.date = i;
    }
}
public class MeiJuDemo {

    public static void main(String[] args) {
        DateDemo dd = new DateDemo();
        Calendar calendar = Calendar.getInstance();
        dd.setSeason(Season.autumn);
        dd.setYear(calendar.get(Calendar.YEAR));
        dd.setMonth((calendar.get(Calendar.MONTH)) + 1);
        dd.setDate(calendar.get(Calendar.DAY_OF_MONTH));
        
        System.out.println("当前日期为:" + dd.getYear() + "年" +
                           dd.getMonth() + "月" + dd.getDate()
                           + "日" + dd.getSeason().getValue());
    }

}

结果输出:
当前日期为:2015年10月13日秋天

你可能感兴趣的:(枚举)