// 상속 가능 클래스
open class Bird(val name: String, val age : Int
, val color: String, val wings: Int, val vol: Int) {
fun fly() = print("i can fly")
// 상속하여 사용 가능
open fun sing(vol : Int) = println("sing " +vol)
}
class Lark(name: String, age: Int, color: String
, wings: Int, vol : Int, lang:String): Bird(name, age, color, wings,vol){
fun speak() = print("i can speak " + lang)
override fun sing(vol : Int) { // 부모 내용과 새로운 기능을 추가
super.sing(vol) // 상위의 내용을 먼저 실행한다
print("my volume " + vol)
speak()
}
}
open class Bird(val name:String, val sing: Int
, val color: String, val age: Int) {
fun mycolor() = print("my color" + color)
}
class Lark : Bird {
constructor(name :String, sing:Int, color:String, age: Int)
: this(name, sing, color, 10) {
println("my name is " + name)
println("my volume is" + sing)
println("my age is " + age)
}
constructor(name : String, sing :Int) : super(name, sing, "Red", 10) {
println("my volume is" + sing)
}
}
fun main() {
val brid = Lark("name", 10)
}
// 상속 가능한 클래스
open class Bird {
open val color = "red"
open fun mycolor() = print("my color " + color)
}
class Lark : Bird() {
override val color = super.color + " & green"
override fun mycolor() = println(color +" is mine")
inner class Female {
fun mycolor() = println(Lark().color + " female")
fun test() {
// 이너 클래스의 mycolor() 실행
mycolor()
// 바깥 클래스의 mycolor() 실행
Lark().mycolor()
// Bird 클래스의 mycolor() 실행
super@Lark.mycolor()
// Bird 클래스의 color 실행
println("[inside] super@Lark:color : ${super@Lark.color}")
}
}
}
fun main() {
val bird = Lark()
bird.Female().test()
}
실행결과는 다음과 같다
red & green female
red & green is mine
my color red & green
[inside] super@Child:color : red
open class Lark {
open val color = "green"
open fun mycolor() = println("Lark color " + color)
}
interface Parrot {
fun mycolor() = println("Parrot color " + color)
fun speak() = println("i can speak korean")
}
class Bird(name : String): Lark(), Parrot {
override fun descrption() = print(name + "'s color is " + color)
fun test() {
description() // teddy's color is green
speak() // i speak korean
super<Parrot>.mycolor() // Parrot color red
super<Lark>.mycolor() // Lark color green
}
}
fun main() {
val bird = Bird("teddy")
bird.test()
}
실행 결과
teddy's color is green
i speak korean
Parrot color red
Lark color green