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

constructor

  • Kotlin의 클래스는 기본생성자(Primary Constructor)와 보조생성자(Secondary Constructor)를 가질 수 있다.

  • Primary Constructor에 어노테이션이나 접근제한자등이 붙어있지 않는 이상 기본적으로 constructor 키워드는 생략이 가능하다.

//primary constructor
class Person(firstName: String, lastName: String, age: Int)
                                || 
class Person constructor(firstName: String, lastName: String, age: Int)
//기본적으로 두개의 사용법은 동일하다.

val person = Person("k", "H", 345)

//어노테이션이나 접근제한자가 필요하다면 이런식으로 붙이면 된다.
class Person public @Inject constructor(firstName: String, lastName: String, age: Int)

//Secondary constructor
//Java 에서의 생성자 Overloading을 생각하면 된다.
class Person(val name: String) { // <- primary constructor
    //클래스 블럭안에 선언
    constructor(name: String, parent: Person) : this(name)  // Kotlin에서는 기본생성자 이외의 생성자에서는...
                                                            // <- primary constructor를 무조건 호출해 주어야 한다.
                                                            // this() 키워드로 primary constructor를 호출해 준다.
                                                            // 기본생성자에 인자값이 있다면 this(인자값) 으로 호출해 준다.
}

class Person() {
    constructor(name: String, age: Int) : this() // primary constructor의 인자가 없는 경우
}

init

  • 클래스가 가지고 있는 내부 함수. 클래스가 생성됨과 동시에 init블록이 수행이 된다.

  • 클래스를 생성해주고 초기에 무언가 수행을 해야 할 것이 있을때 init을 활용하면 좋다.

//init block의 사용
class Person constructor(firstName: String, lastName: String, age: Int) {
    init {
        //Do Something...
    }
}
PreviousabstractNextData Class

Last updated 6 years ago