真实项目中Switch语句的高级写法

普通:

public class Test {

    public static void main(String[] args) {
        String bussType = "01";
        String bussinessType = "0000000";
        Test test = new Test();
        System.out.println(test.transfer(bussType, bussinessType));


    }
    private String transfer(String bussType,String bussinessType){
        switch (bussType) {
            case "01":
                bussinessType="000001";
                break;
            case "02":
                System.out.println("000002");
                bussinessType="000002";
                break;
            case "03":
                bussinessType="000003";
                break;
            case "04":
                bussinessType="000004";
                break;
            default:
                break;
        }
        return bussinessType;
    }
}

  

高级写法:

public class Test {

    public static void main(String[] args) {
        String bussType = "01";
        String bussinessType = "0000000";
        Test test = new Test();
        System.out.println(test.transfer(bussType, bussinessType));


    }
    private String transfer(String bussType,String bussinessType){
        switch (bussType) {
            case "01":
                return "000001";
            case "02":
                return "000002";
            case "03":
                return "000003";
            case "04":
                return "000004";
        }
        return bussinessType;
    }
}

  

posted on 2022-03-10 10:10  ~码铃薯~  阅读(107)  评论(0编辑  收藏  举报

导航