프로그래밍/Kotlin

[Kotlin] primary constructor call expected

DwEnn 2018. 8. 27. 10:39
728x90
반응형

primary constructor call expected



기본 생성자 호출이 필요 합니다


Kotlin 기본 생성자에 대한 호출이 Kotlin 보조 생성자의 정의에 없을 때 발생


 
class Student(var name: String="") {
    var age: Int = 14
    constructor (name: Stringage: Int){
        this.age = age
    }
    fun printMsg(){
        println("Name is $name. Age is $age.");
    }
}



this 키워드를 사용하여 해결


기본 생성자 똑는 기본 생성자를 호출하는 이전 보조 생성자에 대한 호출을 포함한다



class Student(var name: String="") {
    var age: Int = 14
    constructor (name: Stringage: Int): this(name){
        this.age = age
    }
    fun printMsg(){
        println("Name is $name. Age is $age.");
    }
}




+ Reference

https://www.tutorialkart.com/kotlin/handle-kotlin-compilation-error-primary-constructor-call-expected/ >



반응형