Kotlin 基礎語法詳細介紹
創新互聯作為成都網站建設公司,專注成都網站建設、網站設計,有關成都企業網站定制方案、改版、費用等問題,行業涉及成都塑料袋等多個領域,已為上千家企業服務,得到了客戶的尊重與認可。
基礎語法
定義包名
包名的定義應當在源文件的頭部
package my.demo import java.util.* // ...
文件路徑和包名并不要求匹配,源文件可以被放置在文件系統任意位置
參考:包
定義函數
函數有兩個Int類型參數和Int類型返回值:
fun sum(a: Int, b: Int): Int { return a + b }
函數體中只有一個表達式并且作為函數的返回值:
fun sum(a: Int, b: Int) = a + b
函數沒有返回值:
fun printSum(a: Int, b: Int): Unit { print(a + b) }
Unit類型的返回值類型可以省略:
fun printSum(a: Int, b: Int) { print(a + b) }
參見:函數
定義局部變量
定義只讀類型的局部變量:
val a: Int = 1 val b = 1 // `Int` 類型是被編譯器推理出 val c: Int // 當變量的初始值沒有被提供時,需要定義變量的類型 c = 1 // 賦值
可變局部變量
var x = 5 // `Int` 類型是被編譯器推理出的 x += 1
可參見:屬性與變量
注釋
就像Java與JavaScripe,Kotlin也支持行注釋與代碼塊注釋。
// 這是一段行注釋 /* 這是一段代碼塊 注釋 */
不像Java,代碼塊注釋在Kotlin中是可以被疊加的。
參見:Kotlin代碼文檔
使用字符串模板
fun main(args: Array<String>) { if (args.size == 0) return print("First argument: ${args[0]}") }
參見:字符串模板
使用條件表達式
fun max(a: Int, b: Int): Int { if (a > b) return a else return b }
使用 if 作為一個表達式返回值:
fun max(a: Int, b: Int) = if (a > b) a else b
參見:if 表達式
使用可空變量并檢測是否為空
一個引用必須明確的被設置為可為空當其可能為空值時。
返回值為null 如果str 沒有包含數值:
fun parseInt(str: String): Int? { // ... }
函數的返回值可能為空:
fun main(args: Array<String>) { if (args.size < 2) { print("Two integers expected") return } val x = parseInt(args[0]) val y = parseInt(args[1]) // Using `x * y` yields error because they may hold nulls. if (x != null && y != null) { // x and y are automatically cast to non-nullable after null check print(x * y) } }
或者:
// ... if (x == null) { print("Wrong number format in '${args[0]}'") return } if (y == null) { print("Wrong number format in '${args[1]}'") return } // x and y are automatically cast to non-nullable after null check print(x * y)
參見:空安全
使用類型檢測和類型自動轉換
is 操作符用于檢測一個表達式是否是某一種類型,假如可變參數或者屬性被檢測為某一種類型,那么就不在需要明確的類型轉換:
fun getStringLength(obj: Any): Int? { if (obj is String) { // `obj` is automatically cast to `String` in this branch return obj.length } // `obj` is still of type `Any` outside of the type-checked branch return null }
或者:
fun getStringLength(obj: Any): Int? { if (obj !is String) return null // `obj` is automatically cast to `String` in this branch return obj.length }
或者:
fun getStringLength(obj: Any): Int? { // `obj` is automatically cast to `String` on the right-hand side of `&&` if (obj is String && obj.length > 0) return obj.length return null }
參見:類和類型轉換
使用for循環
fun main(args: Array<String>) { for (arg in args) print(arg) }
或者:
for (i in args.indices) print(args[i])
參見:for循環
使用while循環
fun main(args: Array<String>) { var i = 0 while (i < args.size) print(args[i++]) }
參見:while循環
使用when表達式
fun cases(obj: Any) { when (obj) { 1 -> print("One") "Hello" -> print("Greeting") is Long -> print("Long") !is String -> print("Not a string") else -> print("Unknown") } }
參見:when表達式
使用范圍表達式
檢測一個數值是否在一個范圍內,若在則使用in操作符
if (x in 1..y-1) print("OK")
檢測一個數值是否不在一個范圍內,若不在:
if (x !in 0..array.lastIndex) print("Out")
迭代一個數據范圍項:
for (x in 1..5) print(x)
參見:范圍
使用集合
迭代一個集合:
for (name in names) println(name)
檢測一個集合是否包含某個數據項,使用in操作符
if (text in names) // names.contains(text) is called print("Yes")
使用lambda表達式過濾或者轉換一個集合:
names .filter { it.startsWith("A") } .sortedBy { it } .map { it.toUpperCase() } .forEach { print(it) }
參見:高階函數和Lambda表達式
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
本文題目:Kotlin基礎語法詳細介紹
網頁地址:http://m.newbst.com/article20/jeegco.html
成都網站建設公司_創新互聯,為您提供用戶體驗、品牌網站設計、軟件開發、企業建站、ChatGPT、網站設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯