代替IF / THEN / ELSEIF / ELSE的基本结构

int month = 8;
String monthString;
if (month == 1) {
    monthString = "January";
} else if (month == 2) {
    monthString = "February";
}
...  // and so on


拥有它会很好

    int month = 8;
    String monthString;
    switch (month) {
        case 1:  monthString = "January";
                 break;
        case 2:  monthString = "February";
                 break;

        ..... // and so on

        case 12: monthString = "December";
                 break;
        default: monthString = "Invalid month";
                 break;
    }


由于意图明确,这将提高可读性并使其更易于调试。

最佳答案

2018-是的!!!! -如果最后是-好消息-Switch Now supported
YouTube of Session at golden moment的视频中跳到确切的时间

2014年-目前并不令人遗憾。自2009年以来,我一直在等待此功能,以下社区提供的链接强烈要求该功能。

Add "Switch" or "Case" Statement to Apex

关于salesforce - Salesforce的APEX中是否支持SWITCH和CASE语句?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22123415/

10-12 19:12