当前位置:网站首页>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 

}

原网站

版权声明
本文为[yu-Knight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130641027980.html