当前位置:网站首页>Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters

Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters

2022-06-22 15:45:00 Sunshine clover lxgzxj

One 、 Test environment

name edition
operating system win10
CPU12th Gen Intel Core i7-12700H
Memory 16G
JDK1.8.0_171
Scala3.1.2

Two 、 Code

object TestParameterPassing {
    
    def main(args: Array[String]): Unit = {
    
        ValPmtPassing(ExampleFuc(100))
        NamePmtPassing(ExampleFuc(100))
        FuncPmtPassing(ExampleFuc,100)
    }
    
    def ExampleFuc(Num: Int): Int = {
    
        println(" Call the example function once ")
        Num
    }
    
    // Pass value parameter test function 
    def ValPmtPassing(Num: Int): Unit = {
    
        printf(" Pass value parameter function test :%d\n",Num)
        printf(" Pass value parameter function test :%d\n",Num)
        println("+++++++++++++++++++++")
    }
    
    // Pass name parameter test function 
    def NamePmtPassing(Func: => Int): Unit = {
    
        printf(" Passing parameter function test :%d\n",Func)
        printf(" Passing parameter function test :%d\n",Func)
        println("+++++++++++++++++++++")
    }
    
    // Pass function parameters to test functions 
    def FuncPmtPassing(Func: Int => Int, Num: Int): Unit = {
    
        printf(" Pass function parameter function test :%d\n",Func(Num))
        printf(" Pass function parameter function test :%d\n",Func(Num))
        println("+++++++++++++++++++++")
    }
    
}

3、 ... and 、 Running results

 Call the example function once 
 Pass value parameter function test :100
 Pass value parameter function test :100
+++++++++++++++++++++
 Call the example function once 
 Passing parameter function test :100
 Call the example function once 
 Passing parameter function test :100
+++++++++++++++++++++
 Call the example function once 
 Pass function parameter function test :100
 Call the example function once 
 Pass function parameter function test :100
+++++++++++++++++++++

Four 、 Personal understanding

1、 Named parameter :
The name passing parameter will bring the whole function body of the incoming function into , We are in function ValPmtPassing Call two times , So he will call it completely twice ExampleFuc, Output twice : Call the example function once .

2、 Pass value parameters :
The value passing parameter will only be called once at the beginning ExampleFuc, And then bring the return value of the incoming function into , So only output once : Call the example function once .

3、 Transfer function parameters :
The effect of the implementation is the same as that of the name passing parameter , But there is a slight difference in use , Only one incoming parameter is required for a named parameter , Just pass in the method name and the parameters required by the method , The function parameters need to be passed in multiple , In addition to the function name, there are parameters , Convenience upload name parameter is much better .

原网站

版权声明
本文为[Sunshine clover lxgzxj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221428297532.html