kotlin构造器_Kotlin程序| 二级构造器示例

kotlin构造器

二级建造师 (Secondary Constructor)

  • Kotlin class can also declare, Secondary Constructor in the class body, Prefixed with constructor keyboard

    Kotlin类还可以在类主体中声明“ Secondary Constructor”,以构造函数键盘为前缀

  • If the class has a primary constructor then each secondary constructor needs to delegate primary constructor.

    如果类具有主要构造函数,则每个次要构造函数都需要委派主要构造函数。

  • Delegation of another constructor of the same class is done using the 'this' keyword.

    使用'this'关键字可以委派同一类的另一个构造函数。

  • NOTE: Delegation to the primary constructor happens as the first statement of a secondary constructor and code in initializer blocks effectively becomes part of the primary constructor, so all initializer blocks and property initializer is executed before the secondary constructor body.

    注意:委托给主构造函数的过程是辅助构造函数的第一条语句,并且初始化程序块中的代码有效地成为了主构造函数的一部分,因此所有初始化程序块和属性初始化程序都在辅助构造函数主体之前执行。

  • If Kotlin class has no primary constructor, the delegation still happens implicitly, and the initializer blocks are still executed

    如果Kotlin类没有主构造函数,则委派仍会隐式发生,并且初始化程序块仍将执行

演示Kotlin中二级构造器示例的程序 (Program to demonstrate the example of Secondary Constructor in Kotlin)

package com.includehelp

// Declared Class with Parameterized 
// primary constructor
class Animal(name:String){
    // Property Declaration
    private var name: String?=null
    private var type: String?=null

    // Init Block, effectively becomes part of 
    // the primary constructor
    init{
        this.name=name
        println("Init Block")
    }

    // Secondary Constructor, calls/delegates the 
    // primary constructor using 'this'
    constructor(name:String,type:String) : this(name){
        this.type=type
        println("Secondary Constructor")
    }

    //fun to print class property data
    fun printData(){
        println("Animal -> Name : $name  and Type : $type")
    }
}

// Declared class without primary Constructor, 
// delegation still happens implicitly,
// and the initializer blocks are still executed 
// before Secondary Constructor execution
class Sample{
    // First Init Block
    init{
        println("Init Block 1")
    }

    // Secondary Constructor
    constructor(a: Int,b:Int){
        println("Secondary Constructor : A=$a and B=$b")
    }

    // Second Init block
    init {
        println("Init Block 2")
    }
}

// Main function, Entry Point of Program
fun main(args: Array<String>){
    // Create Class object to call Primary constructor 
    // of Animal Class
    val animal = Animal("Cobra")
    animal.printData()

    // Create Class object to call Secondary constructor 
    // of Animal Class
    val animal2 = Animal("Python","Snake")
    animal2.printData()

    // Create Class object to call Secondary constructor 
    // of Sample Class with named parameter
    Sample(a=10, b=20)
}

Output:

输出:

Init Block
Animal -> Name : Cobra  and Type : null
Init Block
Secondary Constructor
Animal -> Name : Python  and Type : Snake
Init Block 1
Init Block 2
Secondary Constructor : A=10 and B=20


翻译自: https://www.includehelp.com/kotlin/example-of-secondary-constructor.aspx

kotlin构造器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值