Kotlin 基礎語法詳細介紹
基礎語法
定義包名
包名的定義應當在源文件的頭部
1
2
3
4
5
|
package my.demo import java.util.* // ... |
文件路徑和包名并不要求匹配,源文件可以被放置在文件系統任意位置
參考:包
定義函數
函數有兩個Int類型參數和Int類型返回值:
1
2
3
|
fun sum(a: Int, b: Int): Int { return a + b } |
函數體中只有一個表達式并且作為函數的返回值:
1
|
fun sum(a: Int, b: Int) = a + b |
函數沒有返回值:
1
2
3
|
fun printSum(a: Int, b: Int): Unit { print(a + b) } |
Unit類型的返回值類型可以省略:
1
2
3
|
fun printSum(a: Int, b: Int) { print(a + b) } |
參見:函數
定義局部變量
定義只讀類型的局部變量:
1
2
3
4
|
val a: Int = 1 val b = 1 // `Int` 類型是被編譯器推理出 val c: Int // 當變量的初始值沒有被提供時,需要定義變量的類型 c = 1 // 賦值 |
可變局部變量
1
2
|
var x = 5 // `Int` 類型是被編譯器推理出的 x += 1 |
可參見:屬性與變量
注釋
就像Java與JavaScripe,Kotlin也支持行注釋與代碼塊注釋。
1
2
3
4
|
// 這是一段行注釋 /* 這是一段代碼塊 注釋 */ |
不像Java,代碼塊注釋在Kotlin中是可以被疊加的。
參見:Kotlin代碼文檔
使用字符串模板
1
2
3
4
5
|
fun main(args: Array<String>) { if (args.size == 0 ) return print( "First argument: ${args[0]}" ) } |
參見:字符串模板
使用條件表達式
1
2
3
4
5
6
|
fun max(a: Int, b: Int): Int { if (a > b) return a else return b } |
使用 if 作為一個表達式返回值:
1
|
fun max(a: Int, b: Int) = if (a > b) a else b |
參見:if 表達式
使用可空變量并檢測是否為空
一個引用必須明確的被設置為可為空當其可能為空值時。
返回值為null 如果str 沒有包含數值:
1
2
3
|
fun parseInt(str: String): Int? { // ... } |
函數的返回值可能為空:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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) } } |
或者:
1
2
3
4
5
6
7
8
9
10
11
12
|
// ... 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 操作符用于檢測一個表達式是否是某一種類型,假如可變參數或者屬性被檢測為某一種類型,那么就不在需要明確的類型轉換:
1
2
3
4
5
6
7
8
9
|
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 } |
或者:
1
2
3
4
5
6
7
|
fun getStringLength(obj: Any): Int? { if (obj !is String) return null // `obj` is automatically cast to `String` in this branch return obj.length } |
或者:
1
2
3
4
5
6
7
|
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循環
1
2
3
4
|
fun main(args: Array<String>) { for (arg in args) print(arg) } |
或者:
1
2
|
for (i in args.indices) print(args[i]) |
參見:for循環
使用while循環
1
2
3
4
5
|
fun main(args: Array<String>) { var i = 0 while (i < args.size) print(args[i++]) } |
參見:while循環
使用when表達式
1
2
3
4
5
6
7
8
9
|
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操作符
1
2
|
if (x in 1 ..y- 1 ) print( "OK" ) |
檢測一個數值是否不在一個范圍內,若不在:
1
2
|
if (x !in 0 ..array.lastIndex) print( "Out" ) |
迭代一個數據范圍項:
1
2
|
for (x in 1 .. 5 ) print(x) |
參見:范圍
使用集合
迭代一個集合:
1
2
|
for (name in names) println(name) |
檢測一個集合是否包含某個數據項,使用in操作符
1
2
|
if (text in names) // names.contains(text) is called print( "Yes" ) |
使用lambda表達式過濾或者轉換一個集合:
1
2
3
4
5
|
names .filter { it.startsWith( "A" ) } .sortedBy { it } .map { it.toUpperCase() } .forEach { print(it) } |
參見:高階函數和Lambda表達式
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/qq_23547831/article/details/52923248