当前位置:网站首页>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、传函数参数:
实现的效果上和传名参数一样,但使用上稍有区别,传名参数只需要一个传入参数,就把方法名和方法需要的参数一并传入,而传函数参数需要传多个,除了函数名还有参数,便捷性上传名参数好很多。
边栏推荐
- Token processing during API encapsulation
- How to use the concat() function of MySQL
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- The IPO of Tian'an technology was terminated: Fosun and Jiuding were shareholders who planned to raise 350million yuan
- 模板特例化 template<>
- What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
- 数据资产管理:数据发现,发现什么,怎么发现?
- 迷宫问题(BFS记录路径)
- js中const定义变量及for-of和for-in
- 山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
猜你喜欢
![[graduation project] Research on emotion analysis based on semi supervised learning and integrated learning](/img/02/33d7b6a5bc01737c5dbeb944202a66.jpg)
[graduation project] Research on emotion analysis based on semi supervised learning and integrated learning

Tree structured binary tree

Live broadcast goes to sea | domestic live broadcast room produces explosive products again. How can "roll out" win the world

ML笔记-matrix fundamental, Gradient Descent

What are strong and weak symbols in embedded systems?

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

晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦

FreeRtos 任务优先级和中断优先级

KEIL仿真和vspd

After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!
随机推荐
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
乱解码nlp
Driving the efficient growth of the manufacturing industry, UFIDA u9 cloud is constantly improving the password behind it
What does Alibaba cloud's cipu release mean for enterprise customers?
大会倒计时 | 亚马逊云科技创新大会邀您一起构建AI新引擎 !
向量3(静态成员)
Sword finger offer46 -- translate numbers into strings
How MySQL modifies the storage engine to InnoDB
What happened to those who didn't go to college
【题目精刷】2023禾赛-FPGA
TDengine 连接器上线 Google Data Studio 应用商店
专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
Database connection pool: Code Directory
KEIL仿真和vspd
个人免签支付方案推荐
英国考虑基于国家安全因素让Arm在伦敦上市
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
Is it difficult for flush to open an account? Is it safe to open an account online?
Phpstudy 2016 build Pikachu range