当前位置:网站首页>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
}
边栏推荐
- Openvscode cloud ide joins rainbow integrated development system
- Splunk query CSV lookup table data dynamic query
- Leetcode medium question my schedule I
- Offer harvester: add and sum two long string numbers (classic interview algorithm question)
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- [go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
- Wang Zijian: is the NFT of Tencent magic core worth buying?
- opencv学习笔记五——梯度计算/边缘检测
- Opencv learning notes II - basic image operations
猜你喜欢
Application of slip ring of shipborne radar antenna
饥荒云服管理脚本
OpenVSCode云端IDE加入Rainbond一体化开发体系
opencv学习笔记五——梯度计算/边缘检测
The truth of robot education in hands-on practice
Unityhub cracking & unity cracking
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Easy to understand SSO
Game attack and defense world reverse
Implement your own dataset using bisenet
随机推荐
拓维信息使用 Rainbond 的云原生落地实践
Train your dataset with swinunet
MES系統,是企業生產的必要選擇
Use of out covariance and in inversion in kotlin
The use of generics and vararg variable parameters in kotlin
The single value view in Splunk uses to replace numeric values with text
归并排序和非比较排序
Golang 编译约束/条件编译 ( // +build <tags> )
Fast parsing intranet penetration escorts the document encryption industry
数据中台落地实施之法
opencv学习笔记四——膨胀/腐蚀/开运算/闭运算
[go ~ 0 to 1] obtain timestamp, time comparison, time format conversion, sleep and timer on the seventh day
Snyk dependency security vulnerability scanning tool
漏洞复现-Fastjson 反序列化
Openvscode cloud ide joins rainbow integrated development system
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
发挥创客教育空间的广泛实用性
GFS分布式文件系统
Bayes' law
Lua programming learning notes