无涯教程-Kotlin - 构造函数

在Kotlin中,构造函数是类似于method的代码块。构造函数以与类相同的名称声明,后跟括号“()”。构造函数用于在创建对象时初始化变量。

主构造函数

主构造函数用于初始化该类。它在类头声明。主构造函数代码用带有可选参数的括号括起来。

让无涯教程看一下主要构造函数的声明示例。在下面的代码中,无涯教程声明一个带有两个参数名称和id的构造函数myClass。参数名称仅是read属性,而id是read和write属性。

class myClass(valname: String,varid: Int) {
   //class body
}

创建myClasss的对象时,它将分别使用“ Ashu”和“ 101”初始化name和id。

class myClass(val name: String, var id: Int) {
}
fun main(args: Array<String>){
  val myclass = myClass ("Ashu", 101)
  println("Name = ${ myclass.name}")
  println("Id = ${ myclass.id}")
}

输出:

Name = Ashu
Id = 101

主构造函数不包含任何代码,初始化程序块用于代码的初始化。该块的前缀为 init 关键字。

让无涯教程使用initialize块重写以上代码:

class myClass(name: String, id: Int) {
  val e_name: String
  var e_id: Int
  init{
    e_name = name.capitalize()
    e_id = id
    println("Name = ${e_name}")
    println("Id = ${e_id}")
  }
}
fun main(args: Array<String>){
  val myclass = myClass ("Ashu", 101)
}

输出:

Name = Ashu
Id = 101

在上面的代码中,创建myclass对象时,参数name和id接受值“ Ashu”和“ 101”。属性名称和id的使用不带“ val”或“ var”,因此不是myClass类的属性。

次构造函数

在Kotlin中,可以在一个类中创建一个或多个辅助构造函数。辅助构造函数是使用“ constructor”关键字创建的。

让无涯教程来看一个辅助构造函数的声明示例。在下面的代码中,无涯教程声明了带有两个参数名称和id的myClass的两个构造函数。

class myClass{

    constructor(id: Int){
        //code 
    }
    constructor(name: String, id: Int){
        //code 
    }
}

让无涯教程看看创建类对象时分配值的辅助构造函数的示例。

class myClass{

    constructor(name: String, id: Int){
       println("Name = ${name}")
       println("Id = ${id}")
    }
}
fun main(args: Array<String>){
   val myclass = myClass ("Ashu", 101)
}

输出:

Name = Ashu
Id = 101

无涯教程还可以在同一类中同时使用主要构造函数和辅助构造函数。通过在同一类中同时使用主构造函数和辅助构造函数,辅助构造函数需要授权给主构造函数。使用this()关键字对同一类中的另一个构造函数进行授权。

例如:

class myClass(password: String){

    constructor(name: String, id: Int, password: String): this(password){
      println("Name = ${name}")
      println("Id = ${id}")
      println("Password = ${password}")
    }
}
fun main(args: Array<String>){
  val myclass = myClass ("Ashu", 101, "mypassword")
}

输出:

Name = Ashu
Id = 101
Password = mypassword

在Kotlin中,一个辅助构造函数可以调用同一类的另一个辅助构造函数。这是通过使用 this()关键字来完成的。

例如:

class myClass{

    constructor(name: String, id: Int): this(name,id, "mypassword"){
      println("this executes next")
      println("Name = ${name}")
      println("Id = ${id}")
    }

    constructor(name: String, id: Int,pass: String){
      println("this executes first")
      println("Name = ${name}")
      println("Id = ${id}")
      println("Password = ${pass}")
    }
}
fun main(args: Array<String>){
      val myclass = myClass ("Ashu", 101)

}

输出:

this executes first
Name = Ashu
Id = 101
Password = mypassword
this executes next
Name = Ashu
Id = 101

在Kotlin中,一个派生类的辅助构造函数可以调用基类的辅助构造函数。这是使用 super 关键字完成的,这是继承的概念。

open class Parent{

    constructor(name: String, id: Int){
        println("this executes first")
        println("Name = ${name}")
        println("Id = ${id}")
    }

    constructor(name: String, id: Int,pass: String){
        println("this executes third")
        println("Name = ${name}")
        println("Id = ${id}")
        println("Password = ${pass}")
    }
}
class Child: Parent{
    constructor(name: String, id: Int): super(name,id){
        println("this executes second")
        println("Name = ${name}")
        println("Id = ${id}")
    }

   constructor(name: String, id: Int,pass: String):super(name,id,"password"){
        println("this executes forth")
        println("Name = ${name}")
        println("Id = ${id}")
        println("Password = ${pass}")
    }
}
fun main(args: Array<String>){
        val obj1 = Child("Ashu", 101)
        val obj2 = Child("Ashu", 101,"mypassword")
}

输出:

this executes first
Name = Ashu
Id = 101
this executes second
Name = Ashu
Id = 101
this executes third
Name = Ashu
Id = 101
Password = password
this executes forth
Name = Ashu
Id = 101
Password = mypassword

Kotlin - 构造函数 - 无涯教程网无涯教程网提供在Kotlin中,构造函数是类似于method的代码块。构造函数以与类相同的名称声明,后跟括...https://www.learnfk.com/kotlin/kotlin-constructor.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值