了解 Kotlin!
var flag = false;
if(condition) {
//do one thing
flag = true;
}
if(condition2) {
//do another thing
flag = true;
}
if(flag)
save();
在 Kotlin 中避免這種flag方法的正確方法是什么?
uj5u.com熱心網友回復:
我在 Kotlin 中沒有看到任何巧妙的方法可以簡潔地做到這一點,但在大多數情況下,我可能更喜歡為變數分配條件而不是這種flag方法:
val isBlue = ... some condition ...
val hasWings = ... another condition ...
if (isBlue) {
...
}
if (hasWings) {
...
}
if (isBlue || hasWings) {
...
}
當然,這是個人口味的問題。
此外,在某些情況下flag更有意義。有save()在你的榜樣,所以如果這個標志是一樣的東西shouldSaveData,它依賴于其中,例如,資料發生突變幾個條件,那么flag方法非常有意義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/379342.html
標籤:科特林
