Kotlin - 构造函数

Kotlin - 构造函数 首页 / Kotlin入门教程 / 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”关键字创建的。

链接:https://www.learnfk.comhttps://www.learnfk.com/kotlin/kotlin-constructor.html

来源:LearnFk无涯教程网

让无涯教程来看一个辅助构造函数的声明示例。在下面的代码中,无涯教程声明了带有两个参数名称和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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出gRPC -〔李林锋〕

Java核心技术面试精讲 -〔杨晓峰〕

趣谈网络协议 -〔刘超〕

技术管理案例课 -〔许健〕

说透区块链 -〔自游〕

如何讲好一堂课 -〔薛雨〕

说透元宇宙 -〔方军〕

Web 3.0入局攻略 -〔郭大治〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

好记忆不如烂笔头。留下您的足迹吧 :)