feat: 同步新增 dive 分类题目 Data classes make life easier

This commit is contained in:
SOR
2026-10-08 11:33:47 +08:00
parent 181207338b
commit 5c1168ca1b
3 changed files with 54 additions and 1 deletions
+1 -1
View File
@@ -46,7 +46,7 @@
## 题目数量
- Alphabet:2
- Dive:0
- Dive:1
- Hyper:0
- Extra:3
@@ -23,6 +23,7 @@ object CatalogStructure {
arrayOf(
"${Classification.ALPHABET}.You need a function, not a mathematical one",
"${Classification.EXTRA}.More expressive functions",
"${Classification.DIVE}.Data classes make life easier",
)
)
@@ -0,0 +1,52 @@
@file:SkipCompile(skip = true)
@file:Suppress("ClassName")
package io.github.sor2171.puzzles.dive
import io.github.sor2171.puzzles.annotation.SkipCompile
import org.junit.jupiter.api.Test
/**
* 外星监考官要求你建立一个结构化的档案记录系统。
* 在 Kotlin 中,`data class`(数据类)专门用于保存数据。
*
* 本关深入考察:
* 1. 声明一个包含默认参数的 `data class`;
* 2. 运用 `copy` 进行局部状态更新并保持不可变性;
* 3. 运用解构声明快速提取属性。
*
* 共两个编写区域。
*/
class `Data classes make life easier` {
data class Prisoner(
// write start 1
// write end 1
)
fun pardon(prisoner: Prisoner): Prisoner {
// write start 2
// write end 2
}
@Test
fun puzzle() {
val prisoner1 = Prisoner(id = 39, name = "Earthling")
val prisoner2 = Prisoner(id = 39, name = "Earthling")
assert(prisoner1 == prisoner2)
assert(prisoner1.toString() == "Prisoner(id=39, name=Earthling, dangerLevel=1, status=Captive)")
val pardoned = pardon(prisoner1)
assert(pardoned.status == "Released")
assert(pardoned.dangerLevel == 0)
assert(pardoned.id == 39 && pardoned.name == "Earthling")
assert(prisoner1.status == "Captive")
val (id, name, danger, status) = pardoned
assert(id == 39 && name == "Earthling" && danger == 0 && status == "Released")
assert(pardoned.component1() == id && pardoned.component2() == name)
}
}