Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

클래스

class 키워드로 클래스를 만들 수 있다.
class Invoice {
}
클래스 선언은 클래스 이름, 클래스 헤더(매개변수 및 기본 생성자)와 중괄호로 묶인 클래스 본문으로 구성된다. 클래스 헤더와 본문은 선택사항이다. 즉 아래와 같이 사용 할 수 있다.
class Empty 

생성자

코틀린은 하나의 기본생성자(primary constructor)와 하나 이상의 보조생성자(secondary constructors)를 가질 수 있다. 기본생성자는 클래스 헤더의 일부분으로 사용하는데, 클래스 이름 뒤에 오며 매개변수를 가질 수도 있다.
class Person constructor(firstName: String) {
}

만약 기본생성자가 어떠한 어노테이션이나 visibility modifier를 가지고 있지 않다면 constructor 키워드를 생략 할 수 있다.

기본 생성자는 코드를 포함 할 수 없다. 대신 초기화 코드는 initializer blocks에 위치해야 한다. init 키워드로 초기화 코드 위치를 설정 할 수 있다.
class Customer(name: String) {
    init {
        logger.info("customer initialized with value ${name}")
    }
}
기본 생성자의 매개변수는 initializer 블럭에서 사용 할 수 있다. 또한 클래스 본문에 선언된 속성을 초기화 하는데 사용 할 수도 있다.
class Customer(name: String) {
    val customerKey = name.toUpperCase()
}
속성을 선언하고, 기본 생성자에서 속성을 초기화 하기 위한 더 간결한 구문을 사용 할 수 있다.
class Customer public @Inject constructor(name: String) { ... }

Secondary Constructors

클래스는 접두사에 constructor를 사용 하는 것으로 secondary constructors를 선언할 수 있다.
class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}