当前位置:网站首页>Scala语言学习-06-传名参数、传值参数、传函数参数的区别
Scala语言学习-06-传名参数、传值参数、传函数参数的区别
2022-06-22 14:28:00 【阳光九叶草LXGZXJ】
一、测试环境
| 名称 | 版本 |
|---|---|
| 操作系统 | win10 |
| CPU | 12th Gen Intel Core i7-12700H |
| 内存 | 16G |
| JDK | 1.8.0_171 |
| Scala | 3.1.2 |
二、代码
object TestParameterPassing {
def main(args: Array[String]): Unit = {
ValPmtPassing(ExampleFuc(100))
NamePmtPassing(ExampleFuc(100))
FuncPmtPassing(ExampleFuc,100)
}
def ExampleFuc(Num: Int): Int = {
println("调用一次例子函数")
Num
}
//传值参数测试函数
def ValPmtPassing(Num: Int): Unit = {
printf("传值参数函数测试:%d\n",Num)
printf("传值参数函数测试:%d\n",Num)
println("+++++++++++++++++++++")
}
//传名参数测试函数
def NamePmtPassing(Func: => Int): Unit = {
printf("传名参数函数测试:%d\n",Func)
printf("传名参数函数测试:%d\n",Func)
println("+++++++++++++++++++++")
}
//传函数参数测试函数
def FuncPmtPassing(Func: Int => Int, Num: Int): Unit = {
printf("传函数参数函数测试:%d\n",Func(Num))
printf("传函数参数函数测试:%d\n",Func(Num))
println("+++++++++++++++++++++")
}
}
三、运行结果
调用一次例子函数
传值参数函数测试:100
传值参数函数测试:100
+++++++++++++++++++++
调用一次例子函数
传名参数函数测试:100
调用一次例子函数
传名参数函数测试:100
+++++++++++++++++++++
调用一次例子函数
传函数参数函数测试:100
调用一次例子函数
传函数参数函数测试:100
+++++++++++++++++++++
四、个人理解
1、传名参数:
传名参数会把传入函数的函数体整个带入,我们在函数ValPmtPassing中调用两次,所以他也会完整调用两次ExampleFuc,输出两次:调用一次例子函数。
2、传值参数:
传值参数只会在开始时调用一次ExampleFuc,再把传入函数的返回值带入,所以只输出一次:调用一次例子函数。
3、传函数参数:
实现的效果上和传名参数一样,但使用上稍有区别,传名参数只需要一个传入参数,就把方法名和方法需要的参数一并传入,而传函数参数需要传多个,除了函数名还有参数,便捷性上传名参数好很多。
边栏推荐
- 鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
- 模板特例化 template<>
- FPGA collects DHT11 temperature and humidity
- Verilog使用inout信号的方法
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- The IPO of Tian'an technology was terminated: Fosun and Jiuding were shareholders who planned to raise 350million yuan
- Mitsubishi manipulator demo program
- TDengine 连接器上线 Google Data Studio 应用商店
- 大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
- FPGA采集DHT11温湿度
猜你喜欢

mysql如何将字段修改为not null

After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!

百行代码实现基于Redis的可靠延迟队列

Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!

How to use the concat() function of MySQL

I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!

推动制造业高效增长,用友U9 cloud不断精进背后的密码

KEIL仿真和vspd

Simulation Keil et vspd

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
随机推荐
All famous network platforms in the world
Ml notes matrix fundamental, gradient descent
keil MDK 中使用虚拟串口调试串口
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
问一下想获取到sqlserver的start_lsn有好的办法吗?
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations
模板特例化 template<>
向量1(类和对象)
What is the value of a website? Why build an independent station
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
Bochs software usage record
What are strong and weak symbols in embedded systems?
PHP built-in protocols (supported and encapsulated protocols)
FPGA collects DHT11 temperature and humidity
还整成这样
The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
还可以这样搞nlp(分类)
NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world
社区文章|MOSN 构建 Subset 优化思路分享