feat: 同步新增 dive 分类题目 Data classes make life easier
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user