当前位置:网站首页>Thirteen forms of lambda in kotlin
Thirteen forms of lambda in kotlin
2022-07-07 08:24:00 【yu-Knight】
//Kotlin in Lambda Thirteen forms of
fun main(args: Array<String>) {
// Declaration of functions
var method1: () -> Unit
var method2: (Int, Int) -> Int
var method3: (String, Boolean) -> Any?
var method4: (Int, Double, Long, String?) -> Boolean
// method4(1, 34.4, 345, null)
// Implementation of function
// Functional Statement + Functional Realization
// :( Parameters ) -> return Mode one ()
// = { Parameters -> return } Mode two {}
// Mode one
// This is a function , Just this function , There are no input and output parameters
val method5 = { println("yuknight") }
method5() // call method5 function
method5.invoke() //method5 Function call itself
// Mode two
// This is a function , Just this function , No input parameters , There are output parameters String type
val method6 = { "yuknight" }
println(method6())
println(method6.invoke())
// Mode three
// This is a function , Just this function , Input parameter is 2 individual Int type , Output parameters String type
val method7 = { number1: Int, number2: Int -> number1.toString() + number2.toString() }
println(method7(5, 2))
// Mode 4 (Int, Int)->Int
var method8 = { number1: Int, number2: Int -> number1 + number2 }
// (Int, Int) -> Int Omit not to write , Automatically infer types
//var method8: (Int, Int) -> Int = { number1: Int, number2: Int -> number1 + number2 }
// Methods five
var method9: (Int) -> String // Declaration of functions
method9 = fun(value: Int): String = value.toString() + "yuknight" // Implementation of function
println(method9.invoke(22))
// Mode 6 Statement + Realization
var method10: (Int, Int) -> String = fun(number1, number2) = number1.toString() + number2.toString()
// println(method10.invoke(2,3))
println(method10(2, 3))
if (method10(2, 3) is String) println(" The output is String") else println(" Output is not String")
// Mode 7
var method11: (String, String) -> Unit = { aStr, bStr -> println("aStr:$aStr, bStr:$bStr") }
method11("122", "333")
method11.invoke("122", "333")
// Mode 8
var method12: (String) -> Int = {
println(" What's coming in is :$it")
34 // The last line returns the value
}
var r = method12.invoke("yuknght")
println(r)
// Mode 9
var method13: (Int) -> Unit = {
when (it) {
1 -> println(" The value passed in is 1")
in 20..60 -> println(" The value passed in is 20 To 60 Number between ")
else -> println(" Are not satisfied ")
}
}
method13.invoke(30)
// Mode 10
var method14: (Int, Int) -> Unit = { _, number2 ->
println(" The second parameter is : $number2")
}
method14(3, 90)
// Mode 11
var method15 = { str: Any -> str }
println(method15("yuknight"))
println(method15(23))
println(method15(false))
// Method 12
var method16 = { sex: Char ->
if (sex == ' male ') println(" The representative is Mr ")
else if (sex == ' Woman ') println(" It means girls ")
else println(" Unknown humans ")
}
method16.invoke(' male ')
// Method 13
var method17:String.()->Unit ={
println(" Call object :$this")
}
"yuknight".method17() // Yes String Type object
}边栏推荐
- The reified keyword in kotlin is used for generics
- MES系统,是企业生产的必要选择
- Snyk 依赖性安全漏洞扫描工具
- Zcmu--1492: problem d (C language)
- Infix keyword infix expression and the use of generic extension function in kotlin
- Give full play to the wide practicality of maker education space
- opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
- Splunk查询csv lookup table数据动态查询
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- Using helm to install rainbow in various kubernetes
猜你喜欢

The truth of robot education in hands-on practice

buureservewp(2)

单元测试报告成功率低

opencv学习笔记二——图像基本操作
![[IELTS speaking] Anna's oral learning records part2](/img/c4/ad7ba2394ee7a52b67b643aa45a7ae.png)
[IELTS speaking] Anna's oral learning records part2

The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

DeiT学习笔记

一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)

CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
随机推荐
Low success rate of unit test report
接口作为参数(接口回调)
Excel import function of jeesite form page
Unityhub cracking & unity cracking
Golang 编译约束/条件编译 ( // +build <tags> )
解析机器人科技发展观对社会研究论
Hisense TV starts the developer mode
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
MES system is a necessary choice for enterprise production
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Interpreting the practical application of maker thinking and mathematics curriculum
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Implement your own dataset using bisenet
Le système mes est un choix nécessaire pour la production de l'entreprise
Rsync remote synchronization
One click deployment of highly available emqx clusters in rainbow
Xcit learning notes
Qinglong panel - today's headlines
SSM 整合
在Rainbond中一键部署高可用 EMQX 集群