Kotlin条件控制语句汇总讲解

Bree ·
更新时间:2024-09-20
· 1528 次阅读

目录

一、if表达式

1、带返回值if表达式

2、if 表达式替代三目运算符

3、多级if表达式

二、When表达式

三、when 表达式的功能增强

1、Kotlin 1.3 版本之前 when

2、Kotlin 1.3 版本之后 when

四、使用 when 表达式替代 if 表达式

总结

Tips:Kotlin 中没有 switch-case。

一、if表达式 1、带返回值if表达式

在 Kotlin 中,if 是一个表达式所以它会返回一个值,表达式的值为表达式作 用域内最后一行的值。这一点和 Java 是不同的, 在 Java 中 if 仅仅是语句。

//一般类似 java 中传统 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)) } //作为表达式则可以这样 fun maxOf(a: Int, b: Int):Int{ return if (a > b) a else b } fun main(args: Array<String>) { println(maxOf(1, 5)) }

if 表达式分支可以是代码块,也可以把作用域内最后一行表达式的值作为该 分支块的值:

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 表达式替代三目运算符

因为在 Kotlin 中 if 表达式是带有返回值的,所以在 Kotlin 中是不需要三目 运算符(xxx ? xxx : xxx), 因为 if 表达式这些都能做到。

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

而在 Kotlin 中则可以直接使用 if 表达式来使用:

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

Tips:如果你使用 if 作为表达式而不是语句(例如:返回它的值或者把它赋 给变量),该表达式需要有 else 分支。

3、多级if表达式

和 Java 一样 Kotlin 也支持 if-else if-else 等多级条件选择,但是一般如 果这种带多级条件选择的 IDE 会提示你使用 when 表达式来替代: 下面这个案例会使用 if-else if-else 来判断变量 number 是何种数据类型:

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 函数需要有返回值的时候,必须要有 else 分支。

二、When表达式

在 Kotlin 中使用 when 表达式替代了类似 C 语言的 switch-case 语句。其中 最简单的形式如下:

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 将它的参数与所有的分支条件顺序比较,直到某个分支满足条 件。 when 既可以被当做表达式使用也可以被当做语句使用。如果它被当做 表达式, 符合条件的分支的值就是整个表达式的值,如果当做语句使用, 则忽略个别分支的值。(像 if 一样,每一个分支可以是一个代码块,它的值 是块中最后的表达式的值。)

Tips:如果其他分支都不满足条件将会求值 else 分支。 如果 when 作为一 个表达式使用,则必须有 else 分支, 除非编译器能够检测出所有的可能情 况都已经覆盖了。

如果很多分支需要用相同的方式处理,则可以把多个分支条件放在一起,用 逗号分隔:

fun eval(any: Any): String = when (any) { is Int, Double, Float, Long, Byte, Short -> "this is number" //多个分 支条件放在一起,用逗号分隔 is Char -> "this is char" else -> "other" }

when 也可以用来取代 if-else if 链。 如果不提供参数,所有的分支条件都是 简单的布尔表达式,而当一个分支的条件为真时则执行该分支:

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 表达式的功能增强

自从 Kotlin1.3 版本后对 when 表达式做了一个写法上的优化,为什么这么 说呢? 仅仅就是写法上的优化,实际上什么都没做. 它主要是解决什么问题 呢?细心的小伙伴会发现,在 Kotlin 1.3 版本之前 when 表达式内部是不能使 用传入的值的。

1、Kotlin 1.3 版本之前 when fun main(args: Array<String>) { val value = getValue() //可以看到 1.3 版本之前需要多出来一步 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 表达式使用代码反编译成 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 表达式条件直接是一个表达式,并用 value 保存了返回值, 实际上相当于把外部那一行缩进来写 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 }

通过对比发现,Kotlin 1.3 前后 when 表达式的增强,仅仅是把原来外部那一行代码,缩 进到 when 里写,然而两次写法反编译的 Java 代码是一致的。

四、使用 when 表达式替代 if 表达式

和 if 表达式一样,when 表达式也是带有返回值的。建议对于多层条件级或嵌 套条件控制的使用建议使用 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") } } //多层级条件使用 when 表达式 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" } 总结

到这里 Kotlin 中的条件控制就阐述完毕了,可以发现 Kotlin 中条件控制和 Java 还是存在着很多的不一样的,需要多多练习多多理解,下一篇我们将进 入 Kotlin 中循环控制。

到此这篇关于Kotlin条件控制语句汇总讲解的文章就介绍到这了,更多相关Kotlin条件控制内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



Kotlin

需要 登录 后方可回复, 如果你还没有账号请 注册新账号