二、when 条件表达式 when 条件表达式 , 可以设置 若干 条件表达式 , 当 条件表达式 满足时 , 可以 执行相应分支的代码 ; 代码示例 : fun main() { val name = "Tom" val type = when(name) { "Tom" -> "猫" "Jerry" -> "老鼠" else -> "未知" } println("type : $type")...
text = when (countType) { is Long -> "少壮不努力" is Double -> "老大徒伤悲" else -> "少侠,加油!" } } 【循环的使用】 (1)遍历循环 Kotlin处理循环语句的时候依旧采用了for和while关键字,只是在具体的用法上有所微调,首先先来看一下for循环,如果使用Java遍历某个队列,可以通过“for(item:list...
when语句还可以使用类型来匹配一个值,并在匹配到指定类型时执行相应的代码块: when(x){ isInt->println("x is an integer") isString->println("x is a string") else->println("x does not match any type") } 在这个例子中,判断x的类型是Int还是String,并打印出相应的结果。 5. ...
The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool. The main thread (that runs the main() function) must wait until our coroutine completes, otherwise the program ends before Hello is printed...
在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) ...
Kotlin:支持函数的默认参数,例如fun printName(name: String, isMale: Boolean = true) {}。 test4("haha")funtest4(name:String,isMale:Boolean=true){println("name=$name, isMale=$isMale")} 此外,类的构造函数也支持默认参数: classPerson(valname:String="Kotlin",valage:Int=20) ...
* If [parent] job is specified, then this supervisor job becomes a child job of its parent and is cancelled when its * parent fails or is cancelled. All this supervisor's children are cancelled in this case, too. The invocation of ...
Type conversion is when you convert the value of one data type to another type.In Kotlin, numeric type conversion is different from Java. For example, it is not possible to convert an Int type to a Long type with the following code:...
is Contact.TextMessage -> sendTextMessage(contact.number, announcement) } } 此时仅仅会报告并不醒目的 IDE 检查,而编译器不会提供任何消息。 从 Kotlin 1.6 开始,会生成以下编译器警告: Non-exhaustive 'when' statements on sealed class/interface will be prohibited in 1.7. Add an 'is InstantMessage'...
This behavior is defined by theJSON library descriptor, which provides a set of properties with default values that can be overridden when the library is imported. To check the descriptors of all supported libraries, see thelibraries repository. ...