package com.wzx.kotlin
fun main() {
val len = "wzx".count()
println(len)
val len2 = "wzx".count() {
it == 'z'
}
val len3 = "wzx".count {
it == 'z'
}
val methodAction: () -> String
methodAction = {
val inputValue = 123
"$inputValue wzx"
}
methodAction()
val methodAction2: (Int, Int, Int) -> String = { number1, number2, number3 ->
val inputValue2 = 123
"$inputValue2 wzx $number1,$number1,$number1"
}
methodAction2(1, 2, 3)
val methodAction3: (Int, Int, Int) -> String = { number1, number2, number3 ->
val inputValue2 = 123
"$inputValue2 wzx $number1,$number1,$number1"
}
methodAction2.invoke(1, 2, 3)
val methodAction4: (String) -> String = { "$it wzx" }
println(methodAction4("www"))
val methodAction5: (Double) -> String = { "$it wzx2" }
println(methodAction5(11.2))
val methodAction6: (Int) -> String = { "" }
val methodAction7 = { v1: Double, v2: Float, v3: Int ->
"v1:$v1,v2:$v2,v1:$v3"
}
methodAction7(11.1, 12.1f, 1)
val method2 = {
1.1f
}
method2()
val method3 = { number: Int ->
number
}
method3(1)
val addResulitMethod = { number1: Int, number2: Int ->
"两数相加的结果:${number1 + number2}"
}
addResulitMethod(1, 1)
val weekResultMethod = { number: Int ->
when (number) {
1 -> "星期一"
2 -> "星期二"
else -> -1
}
}
println(weekResultMethod(2))
login("wzx", "123") { msg: String, code: Int ->
println("最终登录情况:msg:$msg code:$code")
}
login("wzx", "123", { msg: String, code: Int ->
println("最终登录情况:msg:$msg code:$code")
})
login("wzx", "123", responseResult = { msg: String, code: Int ->
println("最终登录情况:msg:$msg code:$code")
})
login("wzx", "123") { msg: String, code: Int ->
println("最终登录情况:msg:$msg code:$code")
}
login2("wzx", "123", ::methodResonseResult)
val obj1 = ::methodResonseResult
val obj2 = obj1
login2("wzx", "123", obj1)
login2("wzx", "123", obj2)
val r = show2("学习KT语言")
val niming_method = show("学习KT语言")
niming_method("wzx", 18)
showPersonInfo("wzx", 18) {
println("显示结果:$it")
}
showPersonInfo("wzx", 18, ::showResultMethod)
}
fun login(user: String, pwd: String, responseResult: (String, Int) -> Unit) {
if (user == "wzx" && pwd == "123") {
responseResult("登录成功", 200)
} else {
responseResult("登录失败", 444)
}
}
inline fun login2(user: String, pwd: String, responseResult: (String, Int) -> Unit) {
if (user == "wzx" && pwd == "123") {
responseResult("登录成功", 200)
} else {
responseResult("登录失败", 444)
}
}
fun methodResonseResult(msg: String, code: Int) {
println("最终登录情况:msg:$msg code:$code")
}
fun show(info: String): (String, Int) -> String {
println("我说show函数 info$info")
return { name: String, age ->
Int
"我就是匿名函数"
}
}
fun show2(info: String): String {
return "我说show函数 info$info"
}
inline fun showPersonInfo(name: String, age: Int, showResult: (String) -> Unit) {
val str = "name:$name,age$age"
showResult(str)
}
fun showResultMethod(str: String) {
println("显示结果:$str")
}