同時,SFDC提供本地引擎,因爲解決方法是使用小型實用程序「框架」作爲開關事件面向對象的「語句」:
Switch-Case Utility
實例:
public with sharing class SwitchCaseExample {
public String result {get; set;}
public static final String MSG_FROM_ACTION_1 = 'invoke action 1';
public static final String MSG_FROM_ACTION_2 = 'invoke action 2';
public static final String MSG_FROM_ACTION_3 = 'invoke action 3';
public static final String MSG_FROM_ACTION_4 = 'invoke action 4';
public void testSwitchCase(String value) {
SwitchCaseHelper sch = new SwitchCaseHelper();
sch.switch(value)
.case('value1', new Action1(this), SwitchCaseHelper.PUT_BREAK)
.case('value2', new Action2(this), SwitchCaseHelper.PUT_CONTINUE)
.case('value3', new Action3(this), SwitchCaseHelper.PUT_BREAK)
.default(new Action4(this));
}
private class Action1 implements ActionContainer {
private SwitchCaseExample outerCtx;
public Action1(SwitchCaseExample outerCtx) {
this.outerCtx = outerCtx;
}
public String doAction() {
outerCtx.result = MSG_FROM_ACTION_1;
return null;
}
}
private class Action2 implements ActionContainer {
private SwitchCaseExample outerCtx;
public Action2(SwitchCaseExample outerCtx) {
this.outerCtx = outerCtx;
}
public String doAction() {
outerCtx.result = MSG_FROM_ACTION_2;
return null;
}
}
private class Action3 implements ActionContainer {
private SwitchCaseExample outerCtx;
public Action3(SwitchCaseExample outerCtx) {
this.outerCtx = outerCtx;
}
public String doAction() {
outerCtx.result = MSG_FROM_ACTION_3;
return null;
}
}
private class Action4 implements ActionContainer {
private SwitchCaseExample outerCtx;
public Action4(SwitchCaseExample outerCtx) {
this.outerCtx = outerCtx;
}
public String doAction() {
outerCtx.result = MSG_FROM_ACTION_4;
return null;
}
}
}
接口:
public interface ActionContainer {
String doAction();
}
和開關的情況下邏輯實現
public with sharing class SwitchCaseHelper {
public static final Boolean PUT_BREAK = true;
public static final Boolean PUT_CONTINUE = false;
public class SwitchCaseException extends Exception {}
public static final String EXCEPTION_MESSAGE = 'Switch-Case construction must have one (and only one) "switch" statement';
@TestVisible
private Object switchOperand;
@TestVisible
private Boolean isCaseAfterBreakStatement;
@TestVisible
private Boolean isPreviousSwitch;
public SwitchCaseHelper() {
isCaseAfterBreakStatement = false;
}
public SwitchCaseHelper switch(Object switchOperand) {
if (isPreviousSwitch != null) {
throw new SwitchCaseException(EXCEPTION_MESSAGE);
}
isPreviousSwitch = true;
this.switchOperand = switchOperand;
return this;
}
public SwitchCaseHelper case(Object caseOperand, ActionContainer container, Boolean hasBreak) {
if (isPreviousSwitch == null) {
throw new SwitchCaseException(EXCEPTION_MESSAGE);
}
if (isPreviousSwitch) {
isPreviousSwitch = false;
}
if (isCaseAfterBreakStatement) {
return this;
}
if (switchOperand.equals(caseOperand)) {
container.doAction();
isCaseAfterBreakStatement = hasBreak;
}
return this;
}
public SwitchCaseHelper default(ActionContainer container) {
if (isPreviousSwitch == null) {
throw new SwitchCaseException(EXCEPTION_MESSAGE);
}
if (!isCaseAfterBreakStatement) {
container.doAction();
}
return this;
}
}
在我看來 - 不會發生的。 'Case'已經是一個關鍵字(標準對象之一)。我很遺憾那些必須編寫一個編譯器才能找出正確用法的傻瓜:P – eyescream
@eyescream - 就此而言,「case」仍然有星號,表示它保留供將來使用,儘管含糊不清案例sObject會恕我直言太混亂 - 以下是[文檔]的鏈接(https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_reserved_words.htm) – HungryBeagle
@eyescream,'Schema.Case' –