枚舉
聲明只有值的枚舉
enum class Color {
RED, GREEN, BLUE
}
此外還可以增加屬性和方法,如果需要在枚舉類中定義方法,要使用分號(hào)把枚舉常量列表和方法定義分開,這也是Kotlin唯一必須使用分號(hào)的地方
enum class Color(val r: Int, val g: Int, val b: Int) {
RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255);
fun rgb() = (r * 256 + g) * 256 + b
}
When
可使用多行表達(dá)式函數(shù)體
fun getRgb(color: Color) =
when (color) {
Color.RED -> "255,0,0"
Color.GREEN -> "0, 255, 0"
Color.BLUE -> "0, 0, 255"
}
上面只會(huì)匹配對(duì)應(yīng)分支,如果需要多個(gè)值合并,則使用逗號(hào)隔開
fun getRgb(color: Color) =
when (color) {
Color.RED, Color.GREEN -> "255,255,0"
Color.BLUE -> "0, 0, 255"
}
when可以使用任何對(duì)象,如下使用set進(jìn)行判斷(不分順序)
fun getRgb(c1: Color, c2: Color) =
when (setOf(c1, c2)) {
setOf(Color.RED, Color.GREEN) -> "255,255,0"
setOf(Color.GREEN, Color.BLUE) -> "0,255,255"
else -> throw Exception("none")
}
如果沒有給when提供參數(shù),則分支條件為布爾表達(dá)式
fun getRgb(c1: Color, c2: Color) =
when {
(c1 == Color.RED && c2 == Color.GREEN) || (c1 == Color.GREEN && c2 == Color.RED) -> "255,255,0"
(c1 == Color.GREEN && c2 == Color.BLUE) || (c1 == Color.BLUE && c2 == Color.GREEN) -> "0,255,255"
else -> throw Exception("none")
}
使用When優(yōu)化if
對(duì)于如下類結(jié)構(gòu)
interface Expr
class Num(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr) : Expr
計(jì)算加法時(shí),使用if如下,代碼塊中的最后表達(dá)式即為返回值,但不適用于函數(shù)(需要顯示return)
fun eval(e: Expr): Int =
if (e is Num) {
e.value
} else if (e is Sum) {
eval(e.left) + eval(e.right)
} else {
throw IllegalArgumentException("")
}
可使用when對(duì)其進(jìn)行優(yōu)化
fun eval(e: Expr): Int =
when (e) {
is Num -> {
e.value
}
is Sum -> {
eval(e.left) + eval(e.right)
}
else -> {
throw IllegalArgumentException("")
}
}
in
可使用in判斷一個(gè)值是否在一個(gè)區(qū)間/集合內(nèi),反之使用 !in
fun isNum(c: Char) = c in '0'..'9'
fun isNotNum(c: Char) = c !in '0'..'9'
println("Kotlin" in setOf("Java", "C"))
可用于when中進(jìn)行判斷
fun recognize(c: Char) = when (c) {
in '0'..'9' -> "digit"
in 'a'..'z' -> "letter"
else -> "not know"
}
可用于比較任何實(shí)現(xiàn)了Comparable接口的對(duì)象,如下比較字符串將按照字母表順序
println("Kotlin" in "Java".."Z")
for
如判斷奇偶數(shù)的函數(shù)
fun isEven(i: Int) = when {
i % 2 == 0 -> "偶數(shù)"
else -> "奇數(shù)"
}
for循環(huán)可使用區(qū)間表示兩個(gè)值之間的間隔,如下分別表示[1,10]、[1,10)
for (i in 1..10) {
print(i)
print("是")
println(isEven(i))
}
for (i in 1 until 10) {
print(i)
print("是")
println(isEven(i))
}
如果需要反向,且設(shè)置步長(可為負(fù)數(shù)),可使用
for (i in 10 downTo 1 step 2) {
print(i)
print("是")
println(isEven(i))
}
還可以用for遍歷集合文章來源:http://www.zghlxwxcb.cn/news/detail-735232.html
val chartBinary = TreeMap<Char, String>()
for (c in 'A'..'D') {
val binary = Integer.toBinaryString(c.toInt())
chartBinary[c] = binary;
}
for ((chat, binary) in chartBinary) {
println("$chat = $binary")
}
如果需要跟蹤下標(biāo),可使用withIndex()文章來源地址http://www.zghlxwxcb.cn/news/detail-735232.html
val list = arrayListOf("A", "B")
for ((index, element) in list.withIndex()) {
println("$index: $element")
}
到了這里,關(guān)于Kotlin基礎(chǔ)——枚舉、When、in、for的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!