變量和函數
變量
// val為常量,一旦賦值就不可變
val a = 10
val a: Int = 10
a = 3 // 報錯// var為變量
var a = 10
a = 3
var b: Int = 20
b = 2
函數
fun add(a: Int, b: Int): Unit {a += b // 報錯,參數默認val
}fun add(a: Int, b: Int) {var x: Int = ax += b
}fun add(a: Int, b: Int): Int {return a + b // 或者val c = a + b retun c
}// 還可以再精簡
fun add(a: Int, b: Int) = a + b// 調用函數
val a: Int = 1
val b: Int = 2
add(a, b) // 返回值為Unit的函數
val c = add(a, b) // 返回值為Int的函數
邏輯控制
if條件語句
val a: Int = 1
val b: Int = 2
var v: Int = 0
if (a > b) {v = a
} else {v = b
}// 也可以寫成
var v = if (a > b) a else b // var v = if (a > b) {a} else {b}等價var v = if (a == 1) a else if (a == 2) b else c
when條件語句
var a: Int = 0
when (a) {1 -> Log.d("tag", "1")2 -> Log.d("tag", "2")else -> Log.d("tag", "else")
}when {a == 1 -> Log.d("tag", "1")a == 2 -> Log.d("tag", "2")else -> Log.d("tag", "else")
}fun get(a: Int) = when(a) {1 -> Log.d("tag", "1")else -> Log.d("tag", "2")
}
循環語句
val range = 1..10 // 1到10閉區間
for (i in 1..10) // 從1到10遍歷
for (i in 1 until 10) // 從1到9遍歷
for (i in 10 downTo 1) // 從10到1遍歷
for (i in 1 until 10 step 2) // 相當于for (int i = 1; i < 10; i += 2)
面向對象
類與對象
open class Person(val age: Int) {init {val x = ageLog.d("tag" , "init x = $x")}constructor(): this(1) {Log.d("tag", "constructor")}fun print() {Log.d("tag", "Person")}
}class Student(val id: Int, age: Int) : Person(age) {init {Log.d("tag", "init student")}
}
接口
interface Study {fun read() { // 默認實現Log.d("tag", "default read")}fun say() {Log.d("tag", "default say")}
}open class Person(val age: Int) {init {val x = ageLog.d("tag" , "init x = $x")}constructor(): this(1) {Log.d("tag", "constructor")}fun print() {Log.d("tag", "Person")}
}class Student(val id: Int, age: Int) : Person(age), Study { // 繼承接口init {Log.d("tag", "init student")}override fun read() { // 重寫Log.d("tag", "student read")}
}fun doStudy(study: Study) { // 調用接口函數study.read()study.say()
}// 調用方法
val student = Student(1, 2)
doStudy(student)
數據類和單例類
data class data(val a: String, val b: String) // 數據類// object單例類,實現為餓漢單例
object get {fun print() {Log.d("tag", "this is a class")}
}// 單例類使用
get.print() // 不需要創建對象// 伴生對象實現懶漢單例
// 第一種實現:頂層val + by lazy
val instance by lazy {Instance()
}class Instance {fun print() {Log.d("tag", "Instance")}
}// 第二種實現:伴生對象實現
class Test private constructor() {companion object {val instance: Test by lazy {Test()}}fun print() {Log.d("tag", "instance")}
}
List(MutableList), ArrayList, Map(MutableMap), Set(MutableSet)
List & MutableList
// 創建
val a: List<Int> = listOf(1, 2, 3) // 不加List<Int>也可以自己推斷
val b: MutableList<Int> = MutableListOf(1, 2, 3) // 同上// 查詢
val x = a[0] // 1
val x = a.indexOf(1) // 0
val x = a.slice(0..1) // [1, 2]// 添加
b.add(1) // 末尾加
b.add(0, 1) // 指定索引加// 刪除
b.remove(2) // 按值刪
b.removeAt(1) // 按索引刪// 排序
val sorted = roList.sorted() // 升序
val desc = roList.sortedDescending()
val custom = roList.sortedBy { -it }// 轉換
val x: List<Int> = b.toList()// 去重
val list = listOf(1,2,2,3)
val unique = list.distinct() // [1,2,3]
```
ArrayList(基本和數組還有List一樣,為List底層)
Set & MutableSet
```kotlin
// 創建
val a: Set<Int> = setOf(1, 2, 3)
val b: MutableSet<Int> = mutableSetOf(1, 2, 3)
val c = linkedSetOf(1, 4, 2) // 有序// 操作
b.add(5)
b.remove(5)// 集合運算
val a = setOf(1,2,3)
val b = setOf(3,4,5)
val union = a union b // [1,2,3,4,5]
val inter = a intersect b // [3]
val diff = a subtract b // [1,2]
```
Map & MutableMap
```kotlin
// 創建
val roMap: Map<String, Int> = mapOf("a" to 1, "b" to 2)
val mutMap: MutableMap<String, Int> = mutableMapOf()
val linkedMap = linkedMapOf("c" to 3, "a" to 1) // 有序// 遍歷
roMap.forEach { (k, v) -> println("$k=$v") }
for ((k, v) in roMap) { ... }// 轉換
val swapped = roMap.map { (k, v) -> v to k }.toMap() // 值變鍵
```