[Kotlin] Multi ways to write constuctor in Kotlin

package com.rsk

class Customer (val name: String, val address: String, var age: Int) {
    // if we don't want to pass in the address
    constructor(name: String, age: Int) : this(name, "", age) // secondary constructor must call primary constructor
}

class AlternativeCustomer (val name: String, var age: Int) {
    var address: String

    // init will run whenever primary constructor run
    init {
        address = ""
    }

    constructor(name: String, address: String, age: Int): this(name, age) {
        this.address = address
    }
}

// Shorter version
class AnotherAlternativeCustomer (val name: String,var age: Int, val address: String = "")
原文地址:https://www.cnblogs.com/Answer1215/p/13885714.html