2019年4月4日木曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 4(classes and objects - A Bit of Class)、BE the Compiler(110)の解答を求めてみる。

コード

// A
class TapeDeck {
    var hasRecorder = false
    fun playTape() {
        println("Tape playing")
    }
    fun recordTape() {
        if (hasRecorder) {
            println("Tape recording")
        }
    }
}
// B
class DVDPlayer(var hasRecorder: Boolean) {
    fun playDVD() {
        println("DVD playing")
    }
    fun recordDVD() {
        if (hasRecorder) {
            println("DVD recording")
        }
    }
}
fun main() {
    val t = TapeDeck()
    t.hasRecorder = true
    t.playTape()
    t.recordTape()

    val d = DVDPlayer(true)
    d.playDVD()
    d.recordDVD()
}

入出力結果

Tape playing
Tape recording
DVD playing
DVD recording

Process finished with exit code 0

0 コメント:

コメントを投稿