亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Kotlin條件控制語(yǔ)句匯總講解

 更新時(shí)間:2022年09月27日 10:25:34   作者:Ken'  
條件控制是每門編程語(yǔ)言中必不可少的,一般就是使用我們所熟知的 ifelse ,來(lái)作為我們代碼邏輯選擇條件控制。 在 Java 中一般使用 ifelse 和 switch-case 來(lái)作為條件控制,而在 Kotlin 中則是使用 if-else 和 when 來(lái)作為條件控制

Tips:Kotlin 中沒(méi)有 switch-case。

一、if表達(dá)式

1、帶返回值if表達(dá)式

在 Kotlin 中,if 是一個(gè)表達(dá)式所以它會(huì)返回一個(gè)值,表達(dá)式的值為表達(dá)式作 用域內(nèi)最后一行的值。這一點(diǎn)和 Java 是不同的, 在 Java 中 if 僅僅是語(yǔ)句。

//一般類似 java 中傳統(tǒng) if 的用法
fun maxOf(a: Int, b: Int): Int {
 if (a > b) {
 return a
 } else {
 return b
 }
}
fun main(args: Array<String>) {
 println(maxOf(1, 5))
}
//作為表達(dá)式則可以這樣
fun maxOf(a: Int, b: Int):Int{
 return if (a > b) a else b
}
fun main(args: Array<String>) {
 println(maxOf(1, 5))
}

if 表達(dá)式分支可以是代碼塊,也可以把作用域內(nèi)最后一行表達(dá)式的值作為該 分支塊的值:

fun maxOf(a: Int, b: Int) = if(a > b) {
 println(a)
 a //返回值為 a
} else {
 println(b)
 b //返回值為 b
}
fun main(args: Array<String>) {
 println(maxOf(1, 5))
}

2、if 表達(dá)式替代三目運(yùn)算符

因?yàn)樵?Kotlin 中 if 表達(dá)式是帶有返回值的,所以在 Kotlin 中是不需要三目 運(yùn)算符(xxx ? xxx : xxx), 因?yàn)?if 表達(dá)式這些都能做到。

//Java 中三目運(yùn)算符
public int maxOf(int a, int b) {
 return a > b ? a : b;
}

而在 Kotlin 中則可以直接使用 if 表達(dá)式來(lái)使用:

//Kotlin 中的 if 表達(dá)式
fun maxOf(a: Int, b: Int) = if (a > b) a else b

Tips:如果你使用 if 作為表達(dá)式而不是語(yǔ)句(例如:返回它的值或者把它賦 給變量),該表達(dá)式需要有 else 分支。

3、多級(jí)if表達(dá)式

和 Java 一樣 Kotlin 也支持 if-else if-else 等多級(jí)條件選擇,但是一般如 果這種帶多級(jí)條件選擇的 IDE 會(huì)提示你使用 when 表達(dá)式來(lái)替代: 下面這個(gè)案例會(huì)使用 if-else if-else 來(lái)判斷變量 number 是何種數(shù)據(jù)類型:

fun eval(number: Number) {
 if (number is Int) {
 println("this is int number")
 } else if (number is Double) {
 prinln("this is double number")
 } else if (number is Float) {
 println("this is float number")
 } else if (number is Long) {
 println("this is long number")
 } else if (number is Byte) {
 println("this is byte number")
 } else if (number is Short) {
 println("this is Short number")
 } else {
 throw IllegalArgumentException("invalid argument")
 }
}

但是需要注意的是 如果 eval 函數(shù)需要有返回值的時(shí)候,必須要有 else 分支。

二、When表達(dá)式

在 Kotlin 中使用 when 表達(dá)式替代了類似 C 語(yǔ)言的 switch-case 語(yǔ)句。其中 最簡(jiǎn)單的形式如下:

fun eval(number: Number): String = when (number) {
 is Int -> "this is int number"
 is Double -> "this is double number"
 is Float -> "ths is float number"
 is Long -> "this is long number"
 is Byte -> "this is byte number"
 is Short -> "this is Short number"
 else -> "invalid number"
}
//多種條件判斷混合形式
fun main(args: Array<String>) {
 println(descript("hello"))
}
fun descript(obj: Any): String = when (obj) {
 1 -> "one"
 "hello" -> "hello word"
 is Long -> "long type"
 !is String -> "is not String"
 else -> {
 "unknown type"
 }
}

when 將它的參數(shù)與所有的分支條件順序比較,直到某個(gè)分支滿足條 件。 when 既可以被當(dāng)做表達(dá)式使用也可以被當(dāng)做語(yǔ)句使用。如果它被當(dāng)做 表達(dá)式, 符合條件的分支的值就是整個(gè)表達(dá)式的值,如果當(dāng)做語(yǔ)句使用, 則忽略個(gè)別分支的值。(像 if 一樣,每一個(gè)分支可以是一個(gè)代碼塊,它的值 是塊中最后的表達(dá)式的值。)

Tips:如果其他分支都不滿足條件將會(huì)求值 else 分支。 如果 when 作為一 個(gè)表達(dá)式使用,則必須有 else 分支, 除非編譯器能夠檢測(cè)出所有的可能情 況都已經(jīng)覆蓋了。

如果很多分支需要用相同的方式處理,則可以把多個(gè)分支條件放在一起,用 逗號(hào)分隔:

fun eval(any: Any): String = when (any) {
 is Int, Double, Float, Long, Byte, Short -> "this is number" //多個(gè)分
支條件放在一起,用逗號(hào)分隔
 is Char -> "this is char"
 else -> "other"
}

when 也可以用來(lái)取代 if-else if 鏈。 如果不提供參數(shù),所有的分支條件都是 簡(jiǎn)單的布爾表達(dá)式,而當(dāng)一個(gè)分支的條件為真時(shí)則執(zhí)行該分支:

fun eval(number: Number) {
 when {
 number.isOdd() -> {
 println("this is odd number")
 }
 number.isEven() -> {
 println("this is even number")
 }
 else -> println("this is invalid number")
 }
}

三、when 表達(dá)式的功能增強(qiáng)

自從 Kotlin1.3 版本后對(duì) when 表達(dá)式做了一個(gè)寫法上的優(yōu)化,為什么這么 說(shuō)呢? 僅僅就是寫法上的優(yōu)化,實(shí)際上什么都沒(méi)做. 它主要是解決什么問(wèn)題 呢?細(xì)心的小伙伴會(huì)發(fā)現(xiàn),在 Kotlin 1.3 版本之前 when 表達(dá)式內(nèi)部是不能使 用傳入的值的。

1、Kotlin 1.3 版本之前 when

fun main(args: Array<String>) {
 val value = getValue() //可以看到 1.3 版本之前需要多出來(lái)一步
 when (value) {
 is Int -> "This is Int Type, value is $value".apply(::println) //
注意這里 when 中獲得 value 不是 when 直接傳入 value,而是 when 外部聲明 value
 is String -> "This is String Type, value is
$value".apply(::println)
 is Double -> "This is Double Type, value is
$value".apply(::println)
 is Float -> "This is Float Type, value is $value".apply(::println)
 else -> "unknown type".apply(::println)
 }
}
fun getValue(): Any {
 return 100F
}

我們可以嘗試把 kotlin 1.3 版本之前 when 表達(dá)式使用代碼反編譯成 Java 代碼:

public static final void main(@NotNull String[] args) {
 Intrinsics.checkParameterIsNotNull(args, "args");
 Object value = getValue();
 String var3;
 if (value instanceof Integer) {
 var3 = "This is Int Type, value is " + value;
 System.out.println(var3);
 } else if (value instanceof String) {
 var3 = "This is String Type, value is " + value;
 System.out.println(var3);
 } else if (value instanceof Double) {
var3 = "This is Double Type, value is " + value;
 System.out.println(var3);
 } else if (value instanceof Float) {
 var3 = "This is Float Type, value is " + value;
 System.out.println(var3);
 } else {
 var3 = "unknown type";
 System.out.println(var3);
 }
 }
 @NotNull
 public static final Object getValue() {
 return 100.0F;
 }

2、Kotlin 1.3 版本之后 when

fun main(args: Array<String>) {
 when (val value = getValue()) {//when 表達(dá)式條件直接是一個(gè)表達(dá)式,并用
value 保存了返回值, 實(shí)際上相當(dāng)于把外部那一行縮進(jìn)來(lái)寫
 is Int -> "This is Int Type, value is $value".apply(::println)
 is String -> "This is String Type, value is
$value".apply(::println)
 is Double -> "This is Double Type, value is
$value".apply(::println)
 is Float -> "This is Float Type, value is $value".apply(::println)
 else -> "unknown type".apply(::println)
 }
}
fun getValue(): Any {
 return 100F
}

通過(guò)對(duì)比發(fā)現(xiàn),Kotlin 1.3 前后 when 表達(dá)式的增強(qiáng),僅僅是把原來(lái)外部那一行代碼,縮 進(jìn)到 when 里寫,然而兩次寫法反編譯的 Java 代碼是一致的。

四、使用 when 表達(dá)式替代 if 表達(dá)式

和 if 表達(dá)式一樣,when 表達(dá)式也是帶有返回值的。建議對(duì)于多層條件級(jí)或嵌 套條件控制的使用建議使用 when 替代 if-else:

fun eval(number: Number) {
if (number is Int) {
 println("this is int number")
 } else if (number is Double) {
 println("this is double number")
 } else if (number is Float) {
 println("this is float number")
 } else if (number is Long) {
 println("this is long number")
 } else if (number is Byte) {
 println("this is byte number")
 } else if (number is Short) {
 println("this is Short number")
 } else {
 throw IllegalArgumentException("invalid argument")
 }
}
//多層級(jí)條件使用 when 表達(dá)式
fun eval(number: Number): String = when (number) {
 is Int -> "this is int number"
 is Double -> "this is double number"
 is Float -> "ths is float number"
 is Long -> "this is long number"
 is Byte -> "this is byte number"
 is Short -> "this is Short number"
 else -> "invalid number"
}

總結(jié)

到這里 Kotlin 中的條件控制就闡述完畢了,可以發(fā)現(xiàn) Kotlin 中條件控制和 Java 還是存在著很多的不一樣的,需要多多練習(xí)多多理解,下一篇我們將進(jìn) 入 Kotlin 中循環(huán)控制。

到此這篇關(guān)于Kotlin條件控制語(yǔ)句匯總講解的文章就介紹到這了,更多相關(guān)Kotlin條件控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論