kotlin
  • Kotlin
  • Variable
  • Function
  • First Class Citizen
  • High Order Function
  • Pure Function
  • Call by Value, Call by Name
  • Null Safe
  • Generic
  • Expression
    • if - else
    • when
    • try - catch
    • while
    • for
  • Class
    • abstract
    • constructor
    • Data Class
    • Default Value
    • enum
    • inheritance
    • interface
    • open
    • property
    • sealed
  • Kotlin Standard
    • let
    • with
    • apply
    • run
    • also
    • takeIf
    • takeUnless
    • use
    • repeat
  • Collections
    • Mutable
    • Immutable
    • Collections
    • Aggregate operations
      • any
      • all
      • count
      • fold
      • foldRight
      • fold vs foldRight
      • forEach
      • forEachIndexed
      • max
      • maxBy
      • min
      • minBy
      • none
      • reduce
      • reduceRight
      • sum
      • sumBy
    • Filtering operations
      • drop
      • dropLast
      • dropWhile
      • dropLastWhile
      • filter
      • filterNot
      • slice
      • take
      • takeLast
      • takeWhile
    • Mapping operations
      • map
      • mapIndexed
      • flatMap
      • groupBy
    • Elements operations
      • contains
      • elementAt
      • elementAtOrElse
      • elementAtOrNull
      • first
      • firstOrNull
      • indexOf
      • indexOfFirst
      • indexOfLast
      • last
      • lastIndexOf
      • lastOrNull
      • single
      • singleOrNull
    • Generation operations
      • partition
      • plus
      • minus
      • zip
      • unzip
    • Ordering operations
      • reversed
      • sorted
      • sortedBy
      • sortedDescending
      • sortedByDescending
Powered by GitBook
On this page
  1. Class

inheritance

  • Kotlin enum에서는 상태를 만들고 아래의 방식으로 추상화를 시킬 수 있다.

  • abstract 로 멤버함수를 만들면 enum 안에있는 하나하나의 각 클래스들은 abstract 함수를 무조건 구현을 해야한다.

  • 외부에서 접근할때는 ProtocolState.WAITING.signal() 이런식으로 접근이 가능하다.

enum class ProtocolState {
    WAITING {
        override fun signal() = TALKING
    },

    TALKING {
        override fun signal() = WAITING
    };

    abstract fun signal(): ProtocolState
}

println(ProtocolState.WAITING)    //WAITING
println(ProtocolState.TALKING)    //TALKING        

println(ProtocolState.WAITING.signal())    //TALKING
println(ProtocolState.TALKING.signal())    //WAITING

  • Kotlin의 class는 기본적으로 final 이다.

  • final class는 상속이 되지 않는다.

  • override할 메소드에는 override키워드를 붙여준다. (Java에서의 @Override와 동일)

  • 상속가능한 class를 만드려면 open, abstract키워드를 사용한다.

  • open과 abstract 모두 Java의 abstract와 비슷하다.

  • open class는 instance를 생성 할 수 있지만 abstract class는 instance를 생성 할 수 없다.

PreviousenumNextinterface

Last updated 6 years ago