函數(shù)
函數(shù)聲明
Kotlin 中的函數(shù)使用 fun 關(guān)鍵字聲明:
fun double(x: Int): Int {
return 2 * x
}
函數(shù)用法
//調(diào)用函數(shù)使用傳統(tǒng)的方法:
val result = double(2)
調(diào)用成員函數(shù)使用點(diǎn)表示法:
Sample().foo() // 創(chuàng)建類 Sample 實(shí)例并調(diào)用 foo