2019年4月14日日曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 7(data classes - Dealing with Data)、BE the Compiler(215)の解答を求めてみる。

コード

// 1つ目は問題ない
// 2つ目

data class Student(
    val firstName: String, val lastName: String,
    val house: String, val year: Int = 1
)

fun main() {
    val s1 = Student("Ron", "Weasley", "Gryffindor")
    val s2 = Student(lastName = "Malfoy", firstName = "Draco", year = 1, house = "HOUSE")
    val s3 = s1.copy(firstName = "Fred", year = 3)
    val s4 = s3.copy(firstName = "George")

    val array = arrayOf(s1, s2, s3, s4)

    for (s in array) {
        println("${s.firstName} ${s.lastName} is in ${s.house} year ${s.year}")
    }
}

入出力結果

Ron Weasley is in Gryffindor year 1
Draco Malfoy is in HOUSE year 1
Fred Weasley is in Gryffindor year 3
George Weasley is in Gryffindor year 3

Process finished with exit code 0

0 コメント:

コメントを投稿

関連コンテンツ