真實專案中Switch語句的高階寫法
阿新 • • 發佈:2022-03-10
普通:
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; } }