發(fā)布于:2021-01-21 16:09:04
0
410
0
Switch case語(yǔ)句是一個(gè)強(qiáng)大的編程特性,它允許您基于變量或表達(dá)式的值來(lái)控制程序流。您可以使用它來(lái)執(zhí)行不同的代碼塊,具體取決于運(yùn)行時(shí)的變量值。下面是Java中switch語(yǔ)句的一個(gè)示例。
public static void switch_demo(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
其工作原理如下:
編譯器為switch case語(yǔ)句生成一個(gè)跳轉(zhuǎn)表。
switch variable/expression被求值一次。
switch語(yǔ)句在跳轉(zhuǎn)表中查找求值的變量/表達(dá)式,并直接決定執(zhí)行哪個(gè)代碼塊。
如果沒有找到匹配項(xiàng),則執(zhí)行默認(rèn)case下的代碼。
在上述示例中,根據(jù)變量month
的值,標(biāo)準(zhǔn)輸出中將顯示不同的消息。在本例中,由于月份=8,所以將以標(biāo)準(zhǔn)輸出打印“August”。
Switch語(yǔ)句
盡管像Java和PHP這樣的流行語(yǔ)言都有內(nèi)置的switch語(yǔ)句,但是您可能會(huì)驚訝地發(fā)現(xiàn)Python語(yǔ)言沒有switch語(yǔ)句。因此,您可能會(huì)嘗試使用一系列if-else-if塊,為switch語(yǔ)句的每個(gè)case使用if條件。
但是,由于跳轉(zhuǎn)表,switch語(yǔ)句比if-else-if梯形圖快得多。它不必按順序計(jì)算每個(gè)條件,只需查找一次已計(jì)算的變量/表達(dá)式,然后直接跳轉(zhuǎn)到相應(yīng)的代碼分支來(lái)執(zhí)行它。
Pythonic實(shí)現(xiàn)switch語(yǔ)句的方法是使用強(qiáng)大的字典映射(也稱為關(guān)聯(lián)數(shù)組),它們提供簡(jiǎn)單的一對(duì)一鍵值映射。
下面是上述switch語(yǔ)句的Python實(shí)現(xiàn)。在下面的示例中,我們創(chuàng)建一個(gè)名為switcher
的字典來(lái)存儲(chǔ)所有類似switch的案例。
def switch_demo(argument):
switcher = {
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December"
}
print switcher.get(argument, "Invalid month")
在上面的示例中,當(dāng)您將一個(gè)參數(shù)傳遞給switch_demo
函數(shù)時(shí),它將對(duì)照switcher
字典映射進(jìn)行查找。如果找到匹配項(xiàng),則打印相關(guān)值,否則打印默認(rèn)字符串(“無(wú)效月份”)。默認(rèn)字符串有助于實(shí)現(xiàn)switch語(yǔ)句的“defaultcase”。
函數(shù)的字典映射
在這里它變得更有趣。Python字典的值可以是任何數(shù)據(jù)類型。因此,您不必局限于使用常量(整數(shù)、字符串),還可以使用函數(shù)名和lambda作為值。
例如,還可以通過創(chuàng)建函數(shù)名作為值的字典來(lái)實(shí)現(xiàn)上述switch語(yǔ)句。在本例中,switcher
是函數(shù)名的字典,而不是字符串。
def one():
return "January"
def two():
return "February"
def three():
return "March"
def four():
return "April"
def five():
return "May"
def six():
return "June"
def seven():
return "July"
def eight():
return "August"
def nine():
return "September"
def ten():
return "October"
def eleven():
return "November"
def twelve():
return "December"
def numbers_to_months(argument):
switcher = {
1: one,
2: two,
3: three,
4: four,
5: five,
6: six,
7: seven,
8: eight,
9: nine,
10: ten,
11: eleven,
12: twelve
}
# Get the function from switcher dictionary
func = switcher.get(argument, lambda: "Invalid month")
# Execute the function
print func()
盡管上面的函數(shù)非常簡(jiǎn)單并且只返回字符串,但是您可以使用這種方法在每個(gè)函數(shù)中執(zhí)行復(fù)雜的代碼塊。
Python方法的優(yōu)點(diǎn)
因?yàn)槟梢栽谶\(yùn)行時(shí)更改Python字典(添加、刪除或更新鍵值對(duì)),所以您可以輕松地動(dòng)態(tài)更改switch語(yǔ)句。舉個(gè)例子:
def zero():
return "zero"
def one():
return "one"
def two():
return "two"
switcher = {
0: zero,
1: one,
2: two
}
def numbers_to_strings(argument):
# Get the function from switcher dictionary
func = switcher.get(argument, "nothing")
# Execute the function
return func()
Input: numbers_to_strings(1)
Output: One
Input: switcher[1]=two #changing the switch case
Input: numbers_to_strings(1)
Output: Two
Switch case是一個(gè)非常有用的編程構(gòu)造,它不僅提供了比if-else語(yǔ)句更好的性能,還為您提供了更易于管理的代碼。如果您覺得Python中缺少switch語(yǔ)句有局限性,那么希望上面的教程能夠幫助您實(shí)現(xiàn)它。
作者介紹
熱門博客推薦