当前位置:网站首页>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、传函数参数:
实现的效果上和传名参数一样,但使用上稍有区别,传名参数只需要一个传入参数,就把方法名和方法需要的参数一并传入,而传函数参数需要传多个,除了函数名还有参数,便捷性上传名参数好很多。
边栏推荐
- 历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
- 基础版现在SQL分析查询不能用了吗?
- 封装api时候token的处理
- 推荐几个AI智能平台
- Ml notes matrix fundamental, gradient descent
- Please, don't be brainwashed. This is the living reality of 90% of Chinese people
- 得物技术复杂 C 端项目的重构实践
- 百行代码实现基于Redis的可靠延迟队列
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- Database connection pool: stress testing
猜你喜欢

加密市场进入寒冬,是“天灾”还是“人祸”?

时隔17年,刘亦菲再次刷屏式爆红:普通人不想被淘汰,也要懂得这件事

What are strong and weak symbols in embedded systems?

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?

How MySQL modifies the storage engine to InnoDB

Database connection pool: stress testing

数据库连接池:连接池功能点的实现

Community article | mosn building subset optimization ideas sharing

那些没考上大学的人,后来过的怎样

FPGA collects DHT11 temperature and humidity
随机推荐
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
类似attention nlp
Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world
数据库连接池:代码目录
Exploration and practice of dewu app data simulation platform
KEIL仿真和vspd
Database connection pool: implementation of connection pool function point
向量2(友元及拷贝构造)
Sdvo:ldso+ semantics, direct French Slam (RAL 2022)
接了个私活项目,一下赚了15250,还有必要做主业吗?
【newman】postman生成漂亮的测试报告
Fast and accurate point cloud registration based on minimizing 3D NDT distance
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
那些没考上大学的人,后来过的怎样
英国考虑基于国家安全因素让Arm在伦敦上市
We will resolutely fight and win the hard battle of flood control and disaster relief and spare no effort to ensure the safety of people's lives and property
Mitsubishi manipulator demo program
UE4 obtains local files through blueprints
TDengine 连接器上线 Google Data Studio 应用商店
Flutter video Le lecteur écoute et joue automatiquement la prochaine chanson