本系列文章來學(xué)習(xí) Kotlin 和 Anko 插件 通過 Kotlin 開發(fā)一個 Android 項目腹暖。
Kotlin-Anko學(xué)習(xí)(1) Kotlin、Anko 介紹
Kotlin-Anko學(xué)習(xí)(2) Kotlin 語法基礎(chǔ)-基本類型
Kotlin-Anko學(xué)習(xí)(3) Kotlin 語法基礎(chǔ)-關(guān)鍵字 package翰萨、Import脏答、if、when、for殖告、while阿蝶、return、break黄绩、continue
Kotlin-Anko學(xué)習(xí)(4) Kotlin語法-類羡洁、繼承、抽象類
Kotlin-Anko學(xué)習(xí)(5) Kotlin語法-屬性爽丹、字段筑煮、接口
Kotlin語法
Kotlin 語法的學(xué)習(xí)僅圍繞著 Android 展開,本來不打算寫基礎(chǔ)語法的粤蝎,自己學(xué)習(xí)后感覺腦袋還是萌萌噠真仲,所以決定記錄一篇自己學(xué)習(xí)筆記,方便后續(xù)查閱初澎,推薦直接通過官網(wǎng)學(xué)習(xí)秸应,官方中文網(wǎng)和官方英文版結(jié)合學(xué)習(xí),有些翻譯過來的不好理解谤狡,還是直接看英文文檔比較好灸眼,并可以提高自己閱讀英語的能力,在語法學(xué)習(xí)階段我采用Kotlin網(wǎng)頁版編譯工練習(xí)墓懂。不足之處請多多留言指正焰宣,相互學(xué)習(xí)。
Kotlin package
Kotlin 中捕仔,在源文件(.kt)的首行通過 package 聲明包名:如下:
package foo.bar
fun baz() {}
class Goo {}
// ……
- Kotlin 中包名與文件路徑可以不同(與java區(qū)別)匕积,用于區(qū)分方法與類的唯一性,沒有指明包榜跌,該文件的內(nèi)容屬于無名字的默認(rèn)包闪唆。
- Kotlin 中 類與函數(shù)可以并列在源文件中存在(與java區(qū)別)。
Kotlin import
Kotlin 中钓葫,包的導(dǎo)入有默認(rèn)導(dǎo)入和 import 指令導(dǎo)入兩種方式
- 默認(rèn)導(dǎo)入的基礎(chǔ)包和 運行在不同的平臺 導(dǎo)入各自的默認(rèn)包悄蕾,如下:
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.* (自 1.1 起)
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
jvm平臺額外基礎(chǔ)包
java.lang.*
kotlin.jvm.* - import 導(dǎo)入有類和其他的聲明 如下:
導(dǎo)入頂層函數(shù)及屬性
導(dǎo)入在對象聲明中聲明的函數(shù)和屬性
導(dǎo)入枚舉常量
Kotlin if表達(dá)式
- 傳統(tǒng)用法
var max = a
if (a < b) max = b
// With else
var max: Int
if (a > b) {
max = a
} else {
max = b
}
- 作為表達(dá)式,代替三元運算符(條件 ? 然后 : 否則)础浮,作為表達(dá)式一定要有else
val max = if (a > b) a else b
- if 的分支可以是代碼塊帆调,最后的表達(dá)式作為該塊的值
val a = 2
val b = 1
val max = if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
print('\n'+max.toString())
}
Kotlin when 表達(dá)式
when 取代了類 C 語言的 switch 操作符 ,when 既可以被當(dāng)做表達(dá)式使用也可以被當(dāng)做語句使用 whend的集中用法如下:
- when 將它的參數(shù)和所有的分支條件順序比較豆同,直到某個分支滿足條件
- 多分支需要用相同的方式處理番刊,則可以把多個分支條件放在一起
- 任意表達(dá)式(而不只是常量)可作為分支條件
- 采用(in)或者不在(!in)檢測一個值是否在區(qū)間中
- 采用(is)或者不是(!is)檢測是否是一個特定的值
//when替代if-else if 偽代碼:
when {
x.isOdd() -> print("x is odd")
x.isEven() -> print("x is even")
else -> print("x is funny")
}
- when 也可以用來取代 if-else if鏈
Kotlin for 循環(huán)
for 循環(huán)可以對任何提供迭代器(iterator)的對象進(jìn)行遍歷,這相當(dāng)于像 C# 這樣的語言中的 foreach 循環(huán)影锈。
- 什么是迭代器對象:有一個成員函數(shù)或者擴(kuò)展函數(shù) iterator() 如下:
public class Array<T> {
// ......
/**
* Creates an iterator for iterating over the elements of the array.
*/
public operator fun iterator(): Iterator<T>
}
public interface Iterator<out T> {
/**
* Returns the next element in the iteration.
*/
public operator fun next(): T
/**
* Returns `true` if the iteration has more elements.
*/
public operator fun hasNext(): Boolean
}
- for循環(huán)的幾種寫法 如下:
//最常見的寫法
for (item: Int in ints) {
// ……
}
//通過索引遍歷一個數(shù)組或者一個 list
for (i in array.indices) {
print(array[i])
}
//庫函數(shù) withIndex
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
While 循環(huán)
while 和 do..while 的使用與java一致:
while (x > 0) {
x--
}
//-----------------------
do {
val y = retrieveData()
} while (y != null) // y 在此處可見
Return芹务、Break和continue
在循環(huán)中的使用與java一致蝉绷,但新增標(biāo)簽的功能,可以限制跳轉(zhuǎn)的位置
- return:默認(rèn)從最直接包圍它的函數(shù)或者匿名函數(shù)返回
- break:終止最直接包圍它的循環(huán)
- continue:繼續(xù)下一次最直接包圍它的循環(huán)
標(biāo)簽(abc@)
-
break 使用標(biāo)簽 用法如下對比:
最外層for添加標(biāo)簽.png
內(nèi)層for添加標(biāo)簽.png
break標(biāo)簽限制跳轉(zhuǎn)到指定標(biāo)簽的循環(huán)點枣抱,continue使用標(biāo)簽跟break用法一樣熔吗,指定循環(huán)的下一次迭代
- return 使用標(biāo)簽
//傳統(tǒng)的使用,從foo()函數(shù)中返回
fun foo() {
ints.forEach {
if (it == 0) return // nonlocal return from inside lambda directly to the caller of foo()
print(it)
}
}
//使用標(biāo)簽佳晶,從lambda表達(dá)式中返回
fun foo() {
ints.forEach lit@ {
if (it == 0) return@lit
print(it)
}
}
//同上磁滚,只不過用隱式調(diào)用
fun foo() {
ints.forEach {
if (it == 0) return@forEach
print(it)
}
}
//同上 lambda表達(dá)式用匿名函數(shù)代替
fun foo() {
ints.forEach(fun(value: Int) {
if (value == 0) return // local return to the caller of the anonymous fun, i.e. the forEach loop
print(value)
})
}
//返回一個值的話,在標(biāo)簽結(jié)束后跟值
return@a 1