YSHUSH

Interface 본문

Coding/Kotlin

Interface

코딩; 2022. 1. 27. 15:42

interface Foo

interface Foo{
    val bar:Int
    fun method(qux:String)
}

class createFoo

class createFoo(val _bar:Int) :Foo{

    override var bar: Int = _bar

    override fun method(qux: String) {
        println("$bar $qux")
    }
}

함수출력부

fun main(args: Array<String>) {

    var cf = createFoo(123)
    cf.bar = 234
    println(cf.method("hi"))

}

 

클래스는 다중상속이 되지 않지만
인터페이스는 다중 상속이 가능하다.

 

interface Bird

interface Bird{
    val wings:Int
    fun fly()
    fun jump(){
        println("Bird jump")
    }
}

interface Horse

interface Horse{
    val maxSpeed:Int
    fun run()
    fun jump(){
        println("Horse jump & max sqeed: $maxSpeed")
    }
}

class Pegasus

class Pegasus: Bird, Horse{
    override val wings: Int = 2
    override val maxSpeed: Int = 100

    override fun fly() {
        println("Fly~")
    }
    override fun run() {
        println("Run!")
    }

    override fun jump() {
        super<Horse>.jump()     // Horse.jump부분 호출
        println("Pegasus Jump!!!")
    }
}

함수출력부

fun main(args: Array<String>) {

    val pegasus = Pegasus()
    pegasus.fly()
    pegasus.run()
    pegasus.jump()
    
}

'Coding > Kotlin' 카테고리의 다른 글

Composition  (0) 2022.01.27
클래스 상속  (0) 2022.01.27
추상클래스(abstract class)  (0) 2022.01.27
상속  (0) 2022.01.27
가위바위보 게임  (0) 2022.01.26