Kotlin constructor (primary constructor)

后端 未结 1 474
一个人的身影
一个人的身影 2021-01-26 18:19

I have a question about Kotlin constructor.

class abc {
    constructor(a: Int)
    constructor(a: Int, e: Int)
}

class def(a: Int) {
    constructor(a: Int, e:         


        
相关标签:
1条回答
  • 2021-01-26 19:00

    The first class doesn't have a primary constructor while the second class has one. Per the documentation for Secondary Constructors you then have to delegate to it.

    If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

    0 讨论(0)
提交回复
热议问题