当前位置:网站首页>Kotlin函数

Kotlin函数

2022-07-05 12:32:00 好奇的菜鸟

一、函数定义


/*
关键字    函数名          参数类型   返回值类型
 ↓        ↓                ↓       ↓      */
fun helloFunction(name: String): String {
    return "Hello $name !"
}/*   ↑
   花括号内为:函数体
*/

简写

fun helloFunction(name: String): String = "Hello $name !"
fun helloFunction(name: String) = "Hello $name !"

二、函数调用

helloFunction("Kotlin")
helloFunction(name = "Kotlin")

三、函数默认参数


fun createUser(
    name: String,
    age: Int,
    gender: Int = 1,
    friendCount: Int = 0,
    feedCount: Int = 0,
    likeCount: Long = 0L,
    commentCount: Int = 0
) {
    //..
}

fun createUser(
    name: String,
    age: Int,
    gender: Int = 1,
    friendCount: Int = 0,
    feedCount: Int = 0,
    likeCount: Long = 0L,
    commentCount: Int = 0
) {
    //..
}

 有默认值得参数可以不传


createUser(
    name = "Tom",
    age = 30,
    commentCount = 3285
)

原网站

版权声明
本文为[好奇的菜鸟]所创,转载请带上原文链接,感谢
https://yjtzfywh.blog.csdn.net/article/details/125611020