Kotlin 基礎(chǔ)語法詳細(xì)介紹
基礎(chǔ)語法
定義包名
包名的定義應(yīng)當(dāng)在源文件的頭部
1
2
3
4
5
|
package my.demo import java.util.* // ... |
文件路徑和包名并不要求匹配,源文件可以被放置在文件系統(tǒng)任意位置
參考:包
定義函數(shù)
函數(shù)有兩個(gè)Int類型參數(shù)和Int類型返回值:
1
2
3
|
fun sum(a: Int, b: Int): Int { return a + b } |
函數(shù)體中只有一個(gè)表達(dá)式并且作為函數(shù)的返回值:
1
|
fun sum(a: Int, b: Int) = a + b |
函數(shù)沒有返回值:
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) } |
參見:函數(shù)
定義局部變量
定義只讀類型的局部變量:
1
2
3
4
|
val a: Int = 1 val b = 1 // `Int` 類型是被編譯器推理出 val c: Int // 當(dāng)變量的初始值沒有被提供時(shí),需要定義變量的類型 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]}" ) } |
參見:字符串模板
使用條件表達(dá)式
1
2
3
4
5
6
|
fun max(a: Int, b: Int): Int { if (a > b) return a else return b } |
使用 if 作為一個(gè)表達(dá)式返回值:
1
|
fun max(a: Int, b: Int) = if (a > b) a else b |
參見:if 表達(dá)式
使用可空變量并檢測(cè)是否為空
一個(gè)引用必須明確的被設(shè)置為可為空當(dāng)其可能為空值時(shí)。
返回值為null 如果str 沒有包含數(shù)值:
1
2
3
|
fun parseInt(str: String): Int? { // ... } |
函數(shù)的返回值可能為空:
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) |
參見:空安全
使用類型檢測(cè)和類型自動(dòng)轉(zhuǎn)換
is 操作符用于檢測(cè)一個(gè)表達(dá)式是否是某一種類型,假如可變參數(shù)或者屬性被檢測(cè)為某一種類型,那么就不在需要明確的類型轉(zhuǎn)換:
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 } |
參見:類和類型轉(zhuǎn)換
使用for循環(huán)
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循環(huán)
使用while循環(huán)
1
2
3
4
5
|
fun main(args: Array<String>) { var i = 0 while (i < args.size) print(args[i++]) } |
參見:while循環(huán)
使用when表達(dá)式
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表達(dá)式
使用范圍表達(dá)式
檢測(cè)一個(gè)數(shù)值是否在一個(gè)范圍內(nèi),若在則使用in操作符
1
2
|
if (x in 1 ..y- 1 ) print( "OK" ) |
檢測(cè)一個(gè)數(shù)值是否不在一個(gè)范圍內(nèi),若不在:
1
2
|
if (x !in 0 ..array.lastIndex) print( "Out" ) |
迭代一個(gè)數(shù)據(jù)范圍項(xiàng):
1
2
|
for (x in 1 .. 5 ) print(x) |
參見:范圍
使用集合
迭代一個(gè)集合:
1
2
|
for (name in names) println(name) |
檢測(cè)一個(gè)集合是否包含某個(gè)數(shù)據(jù)項(xiàng),使用in操作符
1
2
|
if (text in names) // names.contains(text) is called print( "Yes" ) |
使用lambda表達(dá)式過濾或者轉(zhuǎn)換一個(gè)集合:
1
2
3
4
5
|
names .filter { it.startsWith( "A" ) } .sortedBy { it } .map { it.toUpperCase() } .forEach { print(it) } |
參見:高階函數(shù)和Lambda表達(dá)式
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/qq_23547831/article/details/52923248