Skip to content

Commit

Permalink
[ADD/#1] 코틀린 상속(Extends) 실습
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Dec 13, 2023
1 parent d554d49 commit d25f827
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.random.Random
const val num = 20 // Compile Time Constant

fun main() {
practiceClassDataClassGetterSetter()
practiceExtends()
}

private fun practicePrint() {
Expand Down Expand Up @@ -219,3 +219,35 @@ data class Person(
hobby = "농구"
}
}

private fun practiceExtends() {
val dog = Dog()
val cat = Cat()

println(dog.move())
println(cat.move())
}

abstract class Animal {
// 코틀린은 open 키워드를 붙여야 override 가능
open fun move() {
println("이동!")
}
}

class Dog : Animal() {
override fun move() {
println("껑충? 강아지가 왜 껑충 뜀")
}
}

class Cat : Animal() {
override fun move() {
println("고양이는 살금 ㅇㅈ")
}
}

// 기본 클래스는 상속 불가능, open 키워드 필요
open class Human

class SuperMan : Human()

0 comments on commit d25f827

Please sign in to comment.