再写一点

This commit is contained in:
SOR
2026-09-17 21:45:48 +08:00
parent f1c822cd04
commit 20f946382e
2 changed files with 97 additions and 0 deletions
+83
View File
@@ -1,5 +1,86 @@
# 深入 Kotlin
确保你已经了解所有的基础语法,这很重要。
## 更全面的 class 用法
### 更加详细的 class 构造函数与对象初始化
#### 类初始化
在 Kotlin 的类定义中可以写 `init {}` 代码块,它发生在构造函数之后,作用是使用构造函数定义的成员变量值再做一些构造函数没有的操作,比如利用构造函数的参数再做一些细致的变量处理。
#### 构造函数
构造函数有多种写法:
1. 直接写在类名之后
```kotlin
class MyClass(
val a: Int,
private var b: String,
c: Int // 这里的变量 c 仅用于成员初始化,随后被抛弃,不作为成员变量
) {
val d = c * a
// something else
}
```
```kotlin
class MyClass private constructor( // 非私有时,constructor 可省
// ...
) {
// something else
}
```
2. 写在类定义之内
```kotlin
class MyClass {
val d: Int
constructor(
val a: Int,
private var b: String,
c: Int
) {
d = c * a
}
// something else
}
```
3. 构造函数调用其他构造函数
```kotlin
class MyClass(
var name: String?,
val amount: Int
) {
constructor(
val amount: Int
) : this(null, amount)
// something else
}
```
## 文件 IO
## 用 Gradle 加载依赖(外部库)
## Kotlin 反射
```kotlin
```
## 接下来可以看
@@ -7,4 +88,6 @@
[下一篇:Kotlin Springboot](./Kotlin-Springboot.md)
[更进一步:成为 Kotlin 超人](./成为Kotlin超人.md)
[进入 KMP](./进入KMP.md)