gpt4 book ai didi

java - 当每个 case block 都有相同的代码时,在 switch-case 语句中使用什么?

转载 作者:行者123 更新时间:2023-11-29 07:53:23 26 4
gpt4 key购买 nike

我有可以通过两种方式完成的代码:

SWITCH WITHOUT BREAK(用于case中的通用代码)

ResultCodes resCode = ResultCodes.fromResponseCode(resultCode);
switch (resCode) {
case SUCCESS:
if(userIdentity != null)
Logger.logInfo(MODULE, "User Authenticated Successfully, UseIdentity: " +userIdentity);
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = SUCCESS;
break;
case REDIRECT:
url = resultMap.get(WebinKeyConstants.REDIRECTION_URL.val);
Logger.logInfo(MODULE, "Redirecting to URL : " + url);
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = REDIRECT;
break;
case AUTHENTICATION_FAIL:
case USER_ACCOUNT_BLOCKED:
case USER_ACCOUNT_INACTIVE:
case USER_ACCOUNT_SUSPENDED:
case USER_ACCOUNT_TERMINATED:
case USER_ACCOUNT_BLOCKED_ALERT:
case OTP_SEND_SUCCESS:
case USER_PROFILE_NOT_FOUND:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
}

在上面的场景中,只有一个中断意味着所有情况都将执行相同的代码。

WITH BREAK FOR EACH CASE(对于普通代码)

上述场景也可以通过其他方式实现,如下所示

ResultCodes resCode = ResultCodes.fromResponseCode(resultCode);
switch (resCode) {
case SUCCESS:
if(userIdentity != null)
Logger.logInfo(MODULE, "User Authenticated Successfully, UseIdentity: " +userIdentity);
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = SUCCESS;
break;
case REDIRECT:
url = resultMap.get(WebinKeyConstants.REDIRECTION_URL.val);
Logger.logInfo(MODULE, "Redirecting to URL : " + url);
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = REDIRECT;
break;
case AUTHENTICATION_FAIL:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_ACCOUNT_BLOCKED:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_ACCOUNT_INACTIVE:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_ACCOUNT_SUSPENDED:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_ACCOUNT_TERMINATED:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_ACCOUNT_BLOCKED_ALERT:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case OTP_SEND_SUCCESS:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
case USER_PROFILE_NOT_FOUND:
resultMessage=getText(resCode.responseCode.toString());
RESPONSE = ERROR;
break;
}

哪个更好用?有没有性能问题?

最佳答案

第一个switch block代码更少,可读性更好。如果出现性能问题,则可以忽略不计。

关于java - 当每个 case block 都有相同的代码时,在 switch-case 语句中使用什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com