Kotlin
[코틀린] 인터페이스 (Interface)
Sky Titan
2020. 9. 18. 17:44
728x90
인터페이스 (Interface)
- 일종의 구현 약속으로 인터페이스를 구현하는 클래스들은 인터페이스가 가지고 있는 내용을 구현해야함
- 클래스의 형태를 규정한다.
- 추상 클래스와는 다르게 추상 프로퍼티만 가질 수 있다.
- 자바와는 다르게 코틀린의 인터페이스에는 메서드에 구현 내용을 넣을 수 있다.
- 인터페이스 자체로는 객체 생성 x
인터페이스 접근
- 코틀린에선 다중 상속을 지원하지 않음 but 여러 개의 인터페이스를 구현하는 것은 가능
- 그렇기 때문에 여러 개 인터페이스에 중복되는 메서드나 프로퍼티에 접근 시도할 때는 앵글 브리켓<>을 사용하여 접근하려는 인터페이스를 명시해줌
open class TestParent{
//open class는 open 사용안하면 오버라이드 x (final로 설정됨)
open fun introduce()
{
}
}
interface TestInterface{
var t : String //abstract를 붙이지 않아도 추상 프로퍼티
//interface는 다 기본적으로 open
fun introduce(){
}
}
class Test : TestParent(), TestInterface {
override fun introduce() {
TODO("Not yet implemented")
}
fun tell()
{
super<TestParent>.introduce() //TestParent의 introduce
super<TestInterface>.introduce() //TestInterface의 introduce
}
}728x90