[英]How to Change If-else statement into Switch Case?
如何將我的if-else語句更改為Switch Case結構?
if (apiName.equals(APIUrlConstant.getIsRegistrationenabledUrl())) {
IsRegistrationEnableModel isRegistrationEnableModel = gson.fromJson(responsedata, IsRegistrationEnableModel.class);
listener.onTaskPostExecute(isRegistrationEnableModel, requestData, responsedata);
} else if (apiName.equals(APIUrlConstant.AboutUs)) {
// do Something
}else if (apiName.equals(APIUrlConstant.AddContentRating)) {
// do Something
}
apiName是字符串類型。 怎么做?
在JDK 7版本中,可以在switch語句的表達式中使用String對象。
https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html
其實你做不到 切換案例表達式必須是常量表達式,而APIUrlConstant.getIsRegistrationenabledUrl()
不是常量表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.