is Float -> "This is Float Type, value is $value".apply(::println) else -> "unknown type".apply(::println) } } fun getValue(): Any { return 100F } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 4. 使用when代替if else 和if表达式一样,when表达式也是带有返回值的。建...
二、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...
.let{returnleft}//在左上下文中去掉对应元素val newLeft=left.minusKey(key)returnwhen{//如果left中不存在要删除的元素,那么当前CombinedContext就不存在要删除的元素,直接返回当前CombinedContext实例newLeft===left->this//如果left中存在要删除的元素,删除了这个元素后,left变为了空,那么直接返回当前CombinedContext...
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. ...
is Boolean -> println("$e is a boolean") is IntArray-> println("[${e.joinToString()}] is an array of integers") is Double -> println("$e is a double") else -> println("$e is unknown") } } } In the example, we check for a type of a variable in a when expression. ...
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 ...
创建了一个KotlinAndroidTarget对象,并执行dynamicallyApplyWhenAndroidPluginIsApplied方法: internal fun Project.dynamicallyApplyWhenAndroidPluginIsApplied( kotlinAndroidTargetProvider: -> KotlinAndroidTarget, additionalConfiguration: (KotlinAndroidTarget) -> Unit = {} ...
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { task -> if (task.getName() == "compileReleaseKotlin") { kotlinOptions { allWarningsAsErrors = true } println("Add kotlin options when task=" + task) } else { println("Do not add kotlin options when task=" + task...