Kotlin教程:默认和命名参数

2020-04-2020:42:25编程语言入门到精通Comments1,715 views字数 1559阅读模式

Kotlin提供了在函数定义中指定默认参数(参数)的工具。
如果调用函数而不传递任何参数,则使用默认参数作为函数定义的参数。 当使用参数调用函数时,传递参数将用作函数定义中的参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

默认参数示例1:在函数调用中不传递参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

fun main(args: Array<String>) {  
    run()  
}  
fun run(num:Int= 5, latter: Char ='x'){  
    print("parameter in function definition $num and $latter")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

parameter in function definition 5 and x
Shell

在上面的程序中,run()函数调用没有传递参数,它使用在函数定义中的默认参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

默认参数示例2:在函数调用中传递一些参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

fun main(args: Array<String>) {  
    run(3)  
}  
fun run(num:Int= 5, latter: Char ='x'){  
    print("parameter in function definition $num and $latter")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

parameter in function definition 3 and x
Shell

在上面的程序中,run()函数调用带有一个(第一个)参数,函数定义的第一个参数是使用传递给函数的值,它指定传递值为:3。 第二个参数用作默认参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

默认参数示例3:在函数调用中传递所有参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

fun main(args: Array<String>) {  
    run(99,'z')  
}  
fun run(num:Int= 5, latter: Char ='x'){  
    print("parameter in function definition $num and $latter")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

parameter in function definition 99 and z
Shell

由于所有参数都在run()函数调用中传递,因此函数定义的参数使用函数调用中传递的参数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

Kotlin命名参数

在讨论命名参数之前,先对上面的程序中做一些修改。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

fun main(args: Array<String>) {  
    run('a')  
}  
fun run(num:Int= 5, latter: Char ='x'){  
    print("parameter in function definition $num and $latter")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

Error: Kotlin: The character literal does not conform to the expected type Int

在这里,尝试将参数'a'从函数调用传递给函数定义的第二个参数。 但编译器假定参数'a'(Char类型)为第一个参数(Int类型)传递,这会导致程序错误。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

要解决上述问题,可使用命名参数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

命名参数是一个参数,在函数调用中定义参数的名称。 定义函数调用的参数名称,并检查匹配函数定义中的名称然后分配给它。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

Kotlin命名为参数示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

fun main(args: Array<String>) {  
    run(latter='a')  
}  
fun run(num:Int= 5, latter: Char ='x'){  
    print("parameter in function definition $num and $latter")  
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

parameter in function definition 5 and a

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-default-and-named-argument.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18264.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/18264.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定