Kotlin基础学习记录

发布于:2025-07-15 ⋅ 阅读:(16) ⋅ 点赞:(0)

变量和函数

变量

// 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 = a
    x += 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 = age
        Log.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 = age
        Log.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() // 值变键
```


网站公告

今日签到

点亮在社区的每一天
去签到