티스토리 뷰

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/ >



반응형

'프로그래밍 > Kotlin' 카테고리의 다른 글

String API, isBlank() vs isEmpty()  (0) 2020.06.14
[Kotlin] lateinit var is initialized  (0) 2018.08.25
[Kotlin] Kotlin 의 특수기호  (0) 2018.08.25
[Kotlin] lateinit vs lazy  (0) 2018.08.23
[Kotlin] 배열에서 초기화  (0) 2018.08.10
공지사항
최근에 올라온 글