본문 바로가기

kotlin interface

Kotlin - 봉인 클래스와 인터페이스 (Sealed classes and interfaces) https://kotlinlang.org/docs/sealed-classes.html#sealed-classes-and-when-expression Sealed classes and interfaces | Kotlin kotlinlang.org 봉인 클래스(Sealed class)와 인터페이스는 선언된 패키지나 모듈 외에서 이를 상속하는 것을 방지 할 수 있다. 예로 서드파티 클라이언트는 봉인 클래스를 상속 받을 수 없다. 따라서 소스를 관리하는 관리 주체가 작성한 자식 클래스 외에는 둘 수가 없다. 그렇기 때문에 컴파일러는 해당 봉인 클래스의 자식 클래스로 어떤 클래스 들이 있는지 파악 할 수 있다. 쉽게 설명하면 내가 어떤 라이브러리를 개발하면서 JhException라는 봉인 클래스를 만들고 이를 상.. 더보기
Kotlin - 인터페이스 (Interface) https://kotlinlang.org/docs/interfaces.html Interfaces | Kotlin kotlinlang.org Kotlin 에서 인터페이스는 추상 메소드와 구현된 메소드를 모두 가질 수 있다. 상태를 저장 할 수 없다는 점이 추상 클래스(abstract)와 다르다. 인터페이스가 속성을 가질 수는 있지만 속성이 추상화 되어 있거나, 접근자(accessor)를 구현 해야 한다. 선언 과 구현 인터페이스는 interface 키워드를 이용해서 정의하고 클래스에서는 하나 이상의 인터페이스를 구현 할 수 있다. interface MyInterface { fun bar() fun foo() { // body } } class Child: MyInterface { override fun .. 더보기