From 5c1168ca1b98b35775bd5d70e626f7597e3a58c3 Mon Sep 17 00:00:00 2001 From: SOR <3272194354@qq.com> Date: Thu, 8 Oct 2026 11:33:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E6=96=B0=E5=A2=9E=20?= =?UTF-8?q?dive=20=E5=88=86=E7=B1=BB=E9=A2=98=E7=9B=AE=20Data=20classes=20?= =?UTF-8?q?make=20life=20easier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../sor2171/puzzles/utils/CatalogStructure.kt | 1 + .../dive/Data classes make life easier.kt | 52 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/io/github/sor2171/puzzles/dive/Data classes make life easier.kt diff --git a/README.md b/README.md index dd65ee3..04bb3c0 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ ## 题目数量 - Alphabet:2 -- Dive:0 +- Dive:1 - Hyper:0 - Extra:3 \ No newline at end of file diff --git a/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt b/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt index 15d8413..085bbae 100644 --- a/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt +++ b/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt @@ -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", ) ) diff --git a/src/test/kotlin/io/github/sor2171/puzzles/dive/Data classes make life easier.kt b/src/test/kotlin/io/github/sor2171/puzzles/dive/Data classes make life easier.kt new file mode 100644 index 0000000..04b9002 --- /dev/null +++ b/src/test/kotlin/io/github/sor2171/puzzles/dive/Data classes make life easier.kt @@ -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) + } +}