Kotlin 语言基础
知识结构
什么是 Kotlin
Kotlin 是 JetBrains 开发的现代编程语言,2017 年被 Google 宣布为 Android 官方开发语言。
核心特点:
- 简洁:比 Java 代码量少很多
- 安全:内置空安全机制,减少 NullPointerException
- 互操作:与 Java 100% 兼容,可以混合使用
- 多平台:支持 JVM、Android、JS、Native
Hello World
fun main() { println("Hello, Kotlin!")}变量与常量
变量声明
// var: 可变变量var name = "Kotlin"name = "Android" // OK - 可以重新赋值
// val: 不可变变量(类似 Java 的 final)val version = 1.9// version = 2.0 // Error - 不能重新赋值
// 显式类型声明var age: Int = 25val pi: Double = 3.14159val isActive: Boolean = trueval 与 var 对比
| 特性 | val | var |
|---|---|---|
| 可重新赋值 | 不可以 | 可以 |
| 类似 Java | final 变量 | 普通变量 |
| 推荐程度 | 优先使用 | 需要修改时使用 |
// 推荐:优先使用 valval name = "Kotlin" // 不会被修改val list = mutableListOf(1, 2, 3) // 引用不变,内容可变
// 只有需要重新赋值时才用 varvar counter = 0counter++编译时常量
// const val: 编译时常量,只能用于顶层或 object 中const val MAX_COUNT = 100const val APP_NAME = "MyApp"
object Config { const val VERSION = "1.0.0"}基本数据类型
数字类型
// 整数val byte: Byte = 127 // 8位val short: Short = 32767 // 16位val int: Int = 2147483647 // 32位val long: Long = 9223372036854775807L // 64位
// 浮点数val float: Float = 3.14f // 32位val double: Double = 3.14159 // 64位
// 数字字面量val million = 1_000_000 // 下划线分隔,更易读val hex = 0xFF // 十六进制val binary = 0b1010 // 二进制布尔与字符
val isTrue: Boolean = trueval isFalse: Boolean = false
val char: Char = 'A'val unicode: Char = '\u0041' // 也是 'A'字符串
// 普通字符串val str = "Hello, Kotlin"
// 字符串模板val name = "World"println("Hello, $name!") // Hello, World!println("Length: ${name.length}") // Length: 5
// 多行字符串val multiLine = """ Line 1 Line 2 Line 3""".trimIndent()
// 常用操作"hello".uppercase() // HELLO" trim ".trim() // trim"hello".contains("ell") // true"hello".replace("l", "L") // heLLo"a,b,c".split(",") // [a, b, c]"hello".substring(0, 3) // hel空安全
Kotlin 的类型系统区分可空类型和非空类型,从根本上避免空指针异常。
可空类型
// 非空类型:不能赋值为 nullvar name: String = "Kotlin"// name = null // Error - 编译错误
// 可空类型:使用 ? 标记var nullableName: String? = "Kotlin"nullableName = null // OK安全调用
val name: String? = null
// 安全调用操作符 ?.println(name?.length) // null(不会崩溃)
// 链式安全调用val city = user?.address?.city
// Elvis 操作符 ?:(空值合并)val length = name?.length ?: 0val displayName = name ?: "Unknown"
// 非空断言 !!(谨慎使用)val len = name!!.length // 如果 name 为 null,抛出异常let 作用域函数
val name: String? = "Kotlin"
// 仅当非空时执行name?.let { println("Name is $it") println("Length is ${it.length}")}
// 等价于if (name != null) { println("Name is $name")}函数
基本函数
// 普通函数fun add(a: Int, b: Int): Int { return a + b}
// 单表达式函数fun multiply(a: Int, b: Int): Int = a * b
// 返回类型推断fun subtract(a: Int, b: Int) = a - b
// 无返回值(Unit 可省略)fun greet(name: String) { println("Hello, $name!")}默认参数与命名参数
// 默认参数fun greet(name: String, greeting: String = "Hello") { println("$greeting, $name!")}
greet("Alice") // Hello, Alice!greet("Bob", "Hi") // Hi, Bob!
// 命名参数fun createUser(name: String, age: Int, email: String) { println("$name, $age, $email")}
createUser( name = "Alice", email = "alice@example.com", age = 25 // 顺序可以不同)扩展函数
// 为 String 添加扩展函数fun String.addExclamation(): String { return this + "!"}
println("Hello".addExclamation()) // Hello!
// 为 Int 添加扩展函数fun Int.isEven(): Boolean = this % 2 == 0
println(4.isEven()) // trueprintln(5.isEven()) // falseLambda 表达式
// Lambda 基本语法val sum = { a: Int, b: Int -> a + b }println(sum(3, 4)) // 7
// 类型声明在变量上val multiply: (Int, Int) -> Int = { a, b -> a * b }
// 单参数可用 itval double: (Int) -> Int = { it * 2 }println(double(5)) // 10
// 作为函数参数fun operate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b)}
println(operate(10, 5, { x, y -> x + y })) // 15println(operate(10, 5) { x, y -> x - y }) // 5 - 尾随 lambda高阶函数
// 函数作为参数fun repeat(times: Int, action: (Int) -> Unit) { for (i in 0 until times) { action(i) }}
repeat(3) { index -> println("Index: $index")}
// 函数作为返回值fun multiplier(factor: Int): (Int) -> Int { return { number -> number * factor }}
val triple = multiplier(3)println(triple(5)) // 15控制流
if 表达式
// if 作为表达式,可以返回值val max = if (a > b) a else b
// 多分支val grade = if (score >= 90) { "A"} else if (score >= 80) { "B"} else if (score >= 60) { "C"} else { "F"}when 表达式
// 类似 switch,但更强大val result = when (x) { 1 -> "One" 2 -> "Two" 3, 4 -> "Three or Four" in 5..10 -> "Between 5 and 10" else -> "Unknown"}
// 无参数的 whenval description = when { x < 0 -> "Negative" x == 0 -> "Zero" x > 0 -> "Positive" else -> "Unknown"}
// 类型检查fun describe(obj: Any): String = when (obj) { is Int -> "Integer: $obj" is String -> "String of length ${obj.length}" is List<*> -> "List of size ${obj.size}" else -> "Unknown type"}循环
// for 循环for (i in 1..5) { println(i) // 1, 2, 3, 4, 5}
for (i in 5 downTo 1) { println(i) // 5, 4, 3, 2, 1}
for (i in 0 until 5) { println(i) // 0, 1, 2, 3, 4 (不包含5)}
for (i in 0..10 step 2) { println(i) // 0, 2, 4, 6, 8, 10}
// 遍历集合val list = listOf("a", "b", "c")for (item in list) { println(item)}
// 带索引遍历for ((index, value) in list.withIndex()) { println("$index: $value")}
// while 循环var count = 0while (count < 5) { println(count) count++}集合
List
// 不可变列表val list = listOf(1, 2, 3)// list.add(4) // Error - 没有 add 方法
// 可变列表val mutableList = mutableListOf(1, 2, 3)mutableList.add(4)mutableList.remove(1)
// 常用操作list.sizelist.isEmpty()list.first()list.last()list[0]list.contains(2)list.indexOf(2)Set
// 不可变集合val set = setOf(1, 2, 3, 3) // {1, 2, 3}
// 可变集合val mutableSet = mutableSetOf(1, 2, 3)mutableSet.add(4)mutableSet.remove(1)Map
// 不可变 Mapval map = mapOf("a" to 1, "b" to 2)println(map["a"]) // 1
// 可变 Mapval mutableMap = mutableMapOf("a" to 1, "b" to 2)mutableMap["c"] = 3mutableMap.remove("a")
// 遍历for ((key, value) in map) { println("$key -> $value")}集合操作
val numbers = listOf(1, 2, 3, 4, 5)
// 转换numbers.map { it * 2 } // [2, 4, 6, 8, 10]numbers.filter { it > 2 } // [3, 4, 5]numbers.filterNot { it > 2 } // [1, 2]
// 查找numbers.find { it > 3 } // 4 (第一个匹配)numbers.firstOrNull { it > 10 } // nullnumbers.any { it > 3 } // truenumbers.all { it > 0 } // truenumbers.none { it < 0 } // true
// 聚合numbers.sum() // 15numbers.average() // 3.0numbers.max() // 5numbers.min() // 1numbers.count { it > 2 } // 3
// 其他numbers.sorted() // [1, 2, 3, 4, 5]numbers.sortedDescending() // [5, 4, 3, 2, 1]numbers.reversed() // [5, 4, 3, 2, 1]numbers.distinct() // 去重numbers.take(3) // [1, 2, 3]numbers.drop(2) // [3, 4, 5]numbers.chunked(2) // [[1, 2], [3, 4], [5]]
// 链式操作numbers .filter { it > 1 } .map { it * 2 } .sorted() // [4, 6, 8, 10]类与对象
基本类
class Person(val name: String, var age: Int) {
// 次构造函数 constructor(name: String) : this(name, 0)
// 方法 fun introduce() { println("I'm $name, $age years old.") }}
val person = Person("Alice", 25)println(person.name) // Aliceperson.age = 26person.introduce()init 块
class Person(val name: String) { val nameLength: Int
init { println("Person created: $name") nameLength = name.length }}数据类
// 自动生成 equals, hashCode, toString, copy, componentNdata class User(val name: String, val age: Int)
val user1 = User("Alice", 25)val user2 = User("Alice", 25)
println(user1 == user2) // trueprintln(user1.toString()) // User(name=Alice, age=25)
// copy 函数val user3 = user1.copy(age = 26) // User(name=Alice, age=26)
// 解构声明val (name, age) = user1println("$name is $age years old")单例对象
// object 声明单例object Database { val url = "jdbc:mysql://localhost:3306/mydb"
fun connect() { println("Connecting to $url") }}
Database.connect()
// 伴生对象(类似 Java 的静态成员)class MyClass { companion object { const val TAG = "MyClass"
fun create(): MyClass { return MyClass() } }}
println(MyClass.TAG)val instance = MyClass.create()继承
// 默认类是 final,需要 open 关键字允许继承open class Animal(val name: String) { open fun speak() { println("$name makes a sound") }}
class Dog(name: String, val breed: String) : Animal(name) { override fun speak() { println("$name barks") }
fun fetch() { println("$name fetches the ball") }}
val dog = Dog("Buddy", "Golden Retriever")dog.speak() // Buddy barks接口
interface Drawable { fun draw()
// 可以有默认实现 fun describe() { println("This is drawable") }}
interface Clickable { fun click()}
// 实现多个接口class Button : Drawable, Clickable { override fun draw() { println("Drawing button") }
override fun click() { println("Button clicked") }}抽象类
abstract class Shape { abstract fun area(): Double abstract fun perimeter(): Double
fun describe() { println("Area: ${area()}, Perimeter: ${perimeter()}") }}
class Circle(val radius: Double) : Shape() { override fun area() = Math.PI * radius * radius override fun perimeter() = 2 * Math.PI * radius}密封类
// 密封类限制子类的类型sealed class Result { data class Success(val data: String) : Result() data class Error(val message: String) : Result() object Loading : Result()}
fun handleResult(result: Result) { when (result) { is Result.Success -> println("Data: ${result.data}") is Result.Error -> println("Error: ${result.message}") is Result.Loading -> println("Loading...") // 不需要 else,编译器知道所有情况 }}作用域函数
Kotlin 提供了 5 个作用域函数:let、run、with、apply、also。
let
// 常用于空安全检查和转换val name: String? = "Kotlin"
name?.let { println(it.uppercase()) // KOTLIN}
// 链式转换val result = "hello" .let { it.uppercase() } .let { "$it!" }println(result) // HELLO!run
// 对象配置 + 计算结果val result = "hello".run { println(this) uppercase() // 返回值}println(result) // HELLO
// 无接收者的 runval hexString = run { val digits = "0123456789ABCDEF" digits.take(6)}with
// 对已有对象进行多次操作val numbers = mutableListOf(1, 2, 3)
val result = with(numbers) { add(4) add(5) "List has $size elements" // 返回值}println(result) // List has 5 elementsapply
// 对象配置,返回对象本身val person = Person("Alice", 25).apply { age = 26}
// 常用于构建器模式val intent = Intent().apply { action = "com.example.ACTION" putExtra("key", "value")}also
// 附加操作,返回对象本身val numbers = mutableListOf(1, 2, 3).also { println("List created with ${it.size} elements")}
// 用于调试链式调用val result = numbers .also { println("Before: $it") } .map { it * 2 } .also { println("After: $it") }选择指南
| 函数 | 对象引用 | 返回值 | 使用场景 |
|---|---|---|---|
| let | it | Lambda 结果 | 空安全检查、转换 |
| run | this | Lambda 结果 | 对象配置 + 计算 |
| with | this | Lambda 结果 | 对已有对象操作 |
| apply | this | 对象本身 | 对象初始化配置 |
| also | it | 对象本身 | 附加效果、调试 |
异常处理
fun divide(a: Int, b: Int): Int { if (b == 0) { throw IllegalArgumentException("除数不能为零") } return a / b}
// try-catchtry { val result = divide(10, 0) println(result)} catch (e: IllegalArgumentException) { println("Error: ${e.message}")} finally { println("Done")}
// try 作为表达式val result = try { divide(10, 2)} catch (e: Exception) { 0}协程基础
协程是 Kotlin 的轻量级线程,用于简化异步编程。
基本使用
import kotlinx.coroutines.*
fun main() = runBlocking { // 启动协程 launch { delay(1000) // 非阻塞延迟 println("World!") } println("Hello,")}// 输出: Hello, (等待1秒) World!async/await
import kotlinx.coroutines.*
suspend fun fetchUser(): String { delay(1000) return "Alice"}
suspend fun fetchEmail(): String { delay(1000) return "alice@example.com"}
fun main() = runBlocking { // 并行执行 val userDeferred = async { fetchUser() } val emailDeferred = async { fetchEmail() }
val user = userDeferred.await() val email = emailDeferred.await()
println("$user - $email") // 总共约1秒,而非2秒}suspend 函数
// suspend 函数只能在协程或其他 suspend 函数中调用suspend fun loadData(): String { return withContext(Dispatchers.IO) { // 在 IO 线程执行 "Data loaded" }}下一步
学习了 Kotlin 基础后,可以继续学习:
- Android 开发:Jetpack Compose、Android Architecture Components
- 后端开发:Ktor、Spring Boot with Kotlin
- 跨平台开发:Kotlin Multiplatform (KMP)
- 官方文档:kotlinlang.org