開発環境
- macOS Mojave - Apple (OS)
- Windows 10 Pro (OS)
- IntelliJ IDEA CE(Community Edition) (IDE(統合開発環境))
- Kotlin (プログラミング言語)
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 コメント:
コメントを投稿