当前位置:网站首页>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
}
边栏推荐
- Avatary's livedriver trial experience
- 机器人教育在动手实践中的真理
- opencv学习笔记三——图像平滑/去噪处理
- Use of JMeter
- It's too true. There's a reason why I haven't been rich
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- rsync远程同步
- Offer harvester: add and sum two long string numbers (classic interview algorithm question)
- Vulnerability recurrence easy_ tornado
- The largest 3 same digits in the string of leetcode simple question
猜你喜欢
Using helm to install rainbow in various kubernetes
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
eBPF Cilium实战(2) - 底层网络可观测性
漏洞复现-easy_tornado
PVTV2--Pyramid Vision TransformerV2学习笔记
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
[quick start of Digital IC Verification] 14. Basic syntax of SystemVerilog learning 1 (array, queue, structure, enumeration, string... Including practical exercises)
随机推荐
Infix keyword infix expression and the use of generic extension function in kotlin
[IELTS speaking] Anna's oral learning records part2
eBPF Cilium实战(2) - 底层网络可观测性
Tuowei information uses the cloud native landing practice of rainbow
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
机器人教育在动手实践中的真理
[untitled]
Vulnerability recurrence easy_ tornado
漏洞复现-easy_tornado
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
单场带货涨粉10万,农村主播竟将男装卖爆单?
Using nocalhost to develop microservice application on rainbow
rsync远程同步
ZCMU--1396: 队列问题(2)
XCiT学习笔记
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
Rainbond结合NeuVector实践容器安全管理
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
GFS分布式文件系统
The largest 3 same digits in the string of leetcode simple question