Kotlin

Kotlin - 인터페이스

J_Bin 2022. 6. 14. 13:49

*인터페이스(Interface) : 일종의 구현 약속, 인터페이스를 참조하는 클래스는 인터페이스가 가지고 있는 내용을 구현해야한다. 따라서 인터페이스 자체로는 객체로 만들 수 없고 항상 인터페이스를 구현하는 클래스에서 생성해야 한다.

 

 

# 앵글 브래킷(<>)을 사용한 이름 중복 해결

package chap02.section1

/* 인터페이스 참조하기 */

open class A {
    open fun f() = println("A Class f()")       // (3)
    fun a() = println("A Class a()")
}

// Interface는 기본적으로 open이다.
interface B {
    fun f() = println("B Interface f()")        // (4)
    fun b() = println("B Interface b()")        // (2)
}

// A class를 상속받고, B interface를 참조한다.
class C : A(),B{

    // 컴파일되려면 f()가 오버라이딩 되어야한다.
    override fun f() = println("C Class f()")       // (1)

    fun test(){
        f()             // 현재 클래스의 f()
        b()             // interface B의 f()
        super<A>.f()    // A class의 f()
        super<B>.f()    // B interface의 f()
    }


}

fun main(){

    val c = C()
    c.test()



}

'Kotlin' 카테고리의 다른 글

Kotlin - 클래스의 관계  (0) 2022.06.14
Kotlin - 가시성 지시자  (0) 2022.06.14
Kotlin - super와 this  (0) 2022.06.14
Kotlin - 상속과 다형성  (0) 2022.06.14
Kotlin - 클래스 , 생성자  (0) 2022.06.14