如何比较枚举的类型?

借用Week枚举类。
1. switch 和 if
public class WeekCompare {
    public static void main(String[] args){
        Week day = Week.valueOf("Saturday");
       
        //Compare ordinal
        switch(day){
        case Monday:{
            System.out.println("This day is Monday.");
            break;
        }
        case Tuesday:{
            System.out.println("This day is Tuesday.");
            break;
        }
        case Wednesday:{
            System.out.println("This day is Wednesday.");
            break;
        }
        case Thursday:{
            System.out.println("This day is Thursday.");
            break;
        }
        case Friday:{
            System.out.println("This day is Friday.");
            break;
        }
        case Saturday:{
            System.out.println("This day is Saturday.");
            break;
        }
        case Sunday:{
            System.out.println("This day is Sunday.");
            break;
        }
        default:{
            System.out.println("Unkown day.");
            break;
        }
        }
       


        //Compare reference
        if(day == Week.Monday){
            System.out.println("This day is Monday.");
        }else if(day == Week.Tuesday){
            System.out.println("This day is Tuesday.");
        }else if(day == Week.Wednesday){
            System.out.println("This day is Wednesday.");
        }else if(day == Week.Thursday){
            System.out.println("This day is Thursday.");
        }else if(day == Week.Friday){
            System.out.println("This day is Friday.");
        }else if(day == Week.Saturday){
            System.out.println("This day is Saturday.");
        }else if(day == Week.Sunday){
            System.out.println("This day is Sunday.");
        }else{
            System.out.println("Unkown day.");
        }
    }
}

你可能感兴趣的:(如何比较枚举的类型?)