栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java-切换大小写,多个大小写调用同一函数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-切换大小写,多个大小写调用同一函数

您必须

case
为每个String 使用关键字,如下所示:

switch (str) {    //which mean if String equals to    case "apple":      // apple    case "orange":     // or orange    case "pieapple":   // or pieapple        handleFruit();        break;}

编辑02/05/2019

Java 12

从Java 12开始,提出了新的switch case语法,因此要解决此问题,方法如下:

switch (str) {    case "apple", "orange", "pieapple" -> handleFruit();}

现在,您可以选择多个选项,并用逗号分隔,一个箭头

->
然后是您要执行的操作。

另一种语法是:

考虑到每种情况都返回一个值,并且您想在一个变量中设置值,假设

handleFruit()
返回一个
String
旧语法应为:

String result;  //  <-------------------------- declare switch (str) {    //which mean if String equals to    case "apple":      // apple    case "orange":     // or orange    case "pieapple":   // or pieapple        result = handleFruit();  //      <----- then assign        break;}

现在使用Java 12,您可以像这样:

String result = switch (str) { //  <----------- declare and assign in one shot    case "apple", "orange", "pieapple" -> handleFruit();}

不错的语法



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446704.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号