Java基础系列:switch+enum

1 switch

switch(expression){
     
    case value:
        break;
}
  • expression
    变量类型为byte,short,int,char,string
  • value
    必须为字符串常量或字面量

由于case中的值必须为字符串常量或字面量,使用enum构造swtich时,不可以使用Enum.getValue作为case值,因此,可以先在enum中构造一个swtich,然后,再使用swtich。

2 switch+enum

使用switch+enum结构时,先在enum中构造switch结构,返回enum结构,作为case的常量。

2.1 enum

enum Province{
     
    HEILONGJIANG("1", "黑龙江"),
    JILIN("2", "吉林"),
    LIAONING("3", "辽宁"),
    ;

    private String code;

    private String provinceName;

    Province(String code, String provinceName){
     
        this.code = code;
        this.provinceName = provinceName;
    }

    public String getCode(){
     
        return code;
    }

    public String getProvinceName(){
     
        return provinceName;
    }

    public static Province getProvince(String code){
     
        switch(code){
     
            case "1":
                return Province.HEILONGJIANG;
            case "2":
                return Province.JILIN;
            case "3":
                return Province.LIAONING;
            default:
                return Province.HEILONGJIANG;
        }
    }
}

2.2 switch

2.2.1 switch

Province province = Province.getProvince(String.valueOf(i));
switch(province){
     
    case HEILONGJIANG:
        System.out.println("Province name: " + Province.HEILONGJIANG.getProvinceName());
        break;
    case JILIN:
        System.out.println("Province name: " + Province.JILIN.getProvinceName());
        break;
    case LIAONING:
        System.out.println("Province name: " + Province.LIAONING.getProvinceName());
        break;

}

2.2.2 enum

public static Province getProvince(String code){
     
    switch(code){
     
        case "1":
            return Province.HEILONGJIANG;
        case "2":
            return Province.JILIN;
        case "3":
            return Province.LIAONING;
        default:
            return Province.HEILONGJIANG;
    }
}

2.3 mix

package com.company.basicdatatype;

/**
 * Switch + Enum function test.
 * @author xindaqi
 * @since 2020-09-07
 */

enum Province{
     
    HEILONGJIANG("1", "黑龙江"),
    JILIN("2", "吉林"),
    LIAONING("3", "辽宁"),
    ;

    private String code;

    private String provinceName;

    Province(String code, String provinceName){
     
        this.code = code;
        this.provinceName = provinceName;
    }

    public String getCode(){
     
        return code;
    }

    public String getProvinceName(){
     
        return provinceName;
    }

    public static Province getProvince(String code){
     
        switch(code){
     
            case "1":
                return Province.HEILONGJIANG;
            case "2":
                return Province.JILIN;
            case "3":
                return Province.LIAONING;
            default:
                return Province.HEILONGJIANG;
        }
    }
}

public class SwitchEnumTest {
     

    public static void main(String[] args){
     
        for(int i = 1; i <= 3; i++){
     
            Province province = Province.getProvince(String.valueOf(i));
            switch(province){
     
                case HEILONGJIANG:
                    System.out.println("Province name: " + Province.HEILONGJIANG.getProvinceName());
                    break;
                case JILIN:
                    System.out.println("Province name: " + Province.JILIN.getProvinceName());
                    break;
                case LIAONING:
                    System.out.println("Province name: " + Province.LIAONING.getProvinceName());
                    break;

            }
        }

    }

}

3 小结

你可能感兴趣的:(#,Java,ABC,switch,enum)