Java枚举Enum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/newbie0107/article/details/88677427
public enum TestEnum {
    //值一般是大写的字母,多个值之间以逗号分隔。
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public class Client {
    public static void main(String[] args) {
        Day day = Day.FRIDAY;
        System.out.println(day);
    }
}

运行结果: FRIDAY

public enum Day {
    //值一般是大写的字母,多个值之间以逗号分隔。
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
    SATURDAY{
        @Override
        public boolean isRest() {
            return true;
        }
    }, 
    SUNDAY{
        @Override
        public boolean isRest() {
            return true;
        }
    };

    public boolean isRest(){
        return false;
    }
}
public class Client {
    public static void main(String[] args) {
        Day day = Day.SUNDAY;
        System.out.println(day.isRest());
    }
}

运行结果: true;

public enum Month {

    FEBRUARY(2),MAY(5),SEPTEMBER(9),DECEMBER(12);

    private int value;

    private Month(int value){
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}
public class Client {
    public static void main(String[] args) {
        Month month = Month.MAY;
        System.out.println(month.getValue());
    }
}

运行结果: 5

public enum Month {

    FEBRUARY(2,"二月"),MAY(5,"五月"),SEPTEMBER(9,"九月"),DECEMBER(12,"十二月");

    private int value;
    private String desc;

    private Month(int value, String desc){
        this.value = value;
        this.desc = desc;
    }

    public int getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
public class Client {
    public static void main(String[] args) {
        Month month = Month.DECEMBER;
        System.out.println(month.getValue());
        System.out.println(month.getDesc());
    }
}

运行结果:
  12
  十二月

猜你喜欢

转载自blog.csdn.net/newbie0107/article/details/88677427