📜  Kotlin 构造函数

📅  最后修改于: 2022-05-13 01:55:18.814000             🧑  作者: Mango

Kotlin 构造函数

构造函数是一个特殊的成员函数,它在创建类的对象时调用,主要是为了初始化变量或属性。一个类需要有一个构造函数,如果我们不声明构造函数,那么编译器会生成一个默认构造函数。
Kotlin 有两种类型的构造函数——

  1. 主构造函数
  2. 二级构造函数

Kotlin 中的一个类最多可以有一个主构造函数和一个或多个辅助构造函数。主构造函数初始化类,而辅助构造函数用于初始化类并引入一些额外的逻辑。

主构造函数——

主构造函数在类头中初始化,在类名之后,使用构造函数关键字。参数在主构造函数中是可选的。

class Add constructor(val a: Int, val b: Int) {
     // code
}

如果没有指定注释或访问修饰符,则可以省略构造函数关键字。

class Add(val a: Int, val b: Int) {
     // code
}

主构造函数的 Kotlin 程序 –

Java
//main function
fun main(args: Array)
{
    val add = Add(5, 6)
    println("The Sum of numbers 5 and 6 is: ${add.c}")
}
//primary constructor
class Add constructor(a: Int,b:Int)
{
    var c = a+b;
}


Java
fun main(args: Array) {
    val emp = employee(18018, "Sagnik")
}
class employee(emp_id : Int , emp_name: String) {
    val id: Int
    var name: String
 
    // initializer block
    init {
        id = emp_id
        name = emp_name
 
        println("Employee id is: $id")
        println("Employee name: $name")
    }
}


Java
fun main(args: Array) {
    val emp = employee(18018, "Sagnik")
    // default value for emp_name will be used here
    val emp2 = employee(11011)
    // default values for both parameters because no arguments passed
    val emp3 = employee()
 
}
class employee(emp_id : Int = 100 , emp_name: String = "abc") {
    val id: Int
    var name: String
 
    // initializer block
    init {
        id = emp_id
        name = emp_name
 
        print("Employee id is: $id, ")
        println("Employee name: $name")
        println()
    }
}


Java
//main function
fun main(args: Array)
{
    Add(5, 6)
}
//class with one secondary constructor
class Add
{
    constructor(a: Int, b:Int)
    {
        var c = a + b
        println("The sum of numbers 5 and 6 is: ${c}")
    }
}


Java
fun main(args: Array) {
    employee(18018, "Sagnik")
    employee(11011,"Praveen",600000.5)
}
class employee {
 
      constructor (emp_id : Int, emp_name: String ) {
          var id: Int = emp_id
          var name: String = emp_name
          print("Employee id is: $id, ")
          println("Employee name: $name")
          println()
      }
 
       constructor (emp_id : Int, emp_name: String ,emp_salary : Double) {
           var id: Int = emp_id
           var name: String = emp_name
           var salary : Double = emp_salary
           print("Employee id is: $id, ")
           print("Employee name: $name, ")
           println("Employee name: $salary")
       }
}


Java
//main function
fun main(args: Array)
{
    Add(5, 6)
    Add(5, 6, 7)
    Add(5, 6, 7, 8)
}
//class with three secondary constructors
class Add
{
    constructor(a: Int, b: Int)
    {
        var c = a + b
        println("Sum of 5, 6 = ${c}")
    }
    constructor(a: Int, b: Int, c: Int)
    {
        var d = a + b + c
        println("Sum of 5, 6, 7 = ${d}")
    }
    constructor(a: Int, b: Int, c: Int, d: Int)
    {
        var e = a + b + c + d
        println("Sum of 5, 6, 7, 8 = ${e}")
    }
}


Java
//main function
fun main(args: Array)
{
    Add(5,6)
}
class Add {
    // calling another secondary using this
    constructor(a: Int,b:Int) : this(a,b,7) {
        var sumOfTwo = a + b
        println("The sum of two numbers 5 and 6 is: $sumOfTwo")
    }
    // this executes first
    constructor(a: Int, b: Int,c: Int) {
        var sumOfThree = a + b + c
        println("The sum of three numbers 5,6 and 7 is: $sumOfThree")
    }
}


Java
fun main(args: Array) {
    Child(18018, "Sagnik")
}
open class Parent {
    constructor (emp_id: Int, emp_name: String, emp_salary: Double) {
        var id: Int = emp_id
        var name: String = emp_name
        var salary : Double = emp_salary
        println("Employee id is: $id")
        println("Employee name: $name")
        println("Employee salary: $salary")
        println()
    }
}
class Child : Parent {
    constructor (emp_id : Int, emp_name: String):super(emp_id,emp_name,500000.55){
        var id: Int = emp_id
        var name: String = emp_name
        println("Employee id is: $id")
        println("Employee name: $name")
    }
}


输出:

The Sum of two numbers is: 11

解释:
当我们为类创建对象 add 时,值 5 和 6 将传递给构造函数。构造函数参数 a 和 b 分别使用参数 5 和 6 进行初始化。
局部变量 c 包含变量的总和。我们主要使用${add.c}访问构造函数的属性。

带有初始化块的主构造函数——

主构造函数不能包含任何代码,初始化代码可以放在一个单独的初始化块中,以init关键字为前缀。
带有初始化块的主构造函数的 Kotlin 程序 -

Java

fun main(args: Array) {
    val emp = employee(18018, "Sagnik")
}
class employee(emp_id : Int , emp_name: String) {
    val id: Int
    var name: String
 
    // initializer block
    init {
        id = emp_id
        name = emp_name
 
        println("Employee id is: $id")
        println("Employee name: $name")
    }
}

输出:

Employee id is: 18018
Employee name: Sagnik

解释:
当为类employee创建对象emp时,值18018和“Sagnik”传递给构造函数的参数emp_idemp_name 。在类 id 和 name 中声明了两个属性。
初始化程序块在对象创建时执行,不仅初始化属性,而且打印到标准输出。

主构造函数中的默认值 -

类似于函数中的函数默认值,我们可以用一些默认值来初始化构造函数参数。
主构造函数中默认值的 Kotlin 程序 -

Java

fun main(args: Array) {
    val emp = employee(18018, "Sagnik")
    // default value for emp_name will be used here
    val emp2 = employee(11011)
    // default values for both parameters because no arguments passed
    val emp3 = employee()
 
}
class employee(emp_id : Int = 100 , emp_name: String = "abc") {
    val id: Int
    var name: String
 
    // initializer block
    init {
        id = emp_id
        name = emp_name
 
        print("Employee id is: $id, ")
        println("Employee name: $name")
        println()
    }
}

输出:

Employee id is: 18018, Employee name: Sagnik

Employee id is: 11011, Employee name: abc

Employee id is: 100, Employee name: abc

解释:
在这里,我们使用一些默认值 emp_id = 100 和 emp_name = “abc” 初始化了构造函数参数。
创建对象emp时,我们传递了两个参数的值,因此它会打印这些值。
但是,在创建对象emp2时,我们还没有传递 emp_name,因此初始化程序块使用默认值并打印到标准输出。

二级构造函数——

如上所述,Kotlin 可能有一个或多个辅助构造函数。辅助构造函数允许初始化变量并允许为类提供一些逻辑。它们以构造函数关键字为前缀。
Kotlin 实现二级构造函数的程序——

Java

//main function
fun main(args: Array)
{
    Add(5, 6)
}
//class with one secondary constructor
class Add
{
    constructor(a: Int, b:Int)
    {
        var c = a + b
        println("The sum of numbers 5 and 6 is: ${c}")
    }
}

输出:

The sum of numbers 5 and 6 is: 11

将调用哪个辅助构造函数由编译器根据收到的参数决定。在上面的程序中,我们没有指定调用哪个构造函数和编译器自己决定。
一个类中两个二级构造函数的 Kotlin 程序-

Java

fun main(args: Array) {
    employee(18018, "Sagnik")
    employee(11011,"Praveen",600000.5)
}
class employee {
 
      constructor (emp_id : Int, emp_name: String ) {
          var id: Int = emp_id
          var name: String = emp_name
          print("Employee id is: $id, ")
          println("Employee name: $name")
          println()
      }
 
       constructor (emp_id : Int, emp_name: String ,emp_salary : Double) {
           var id: Int = emp_id
           var name: String = emp_name
           var salary : Double = emp_salary
           print("Employee id is: $id, ")
           print("Employee name: $name, ")
           println("Employee name: $salary")
       }
}

输出:

Employee id is: 18018, Employee name: Sagnik

Employee id is: 11011, Employee name: Praveen, Employee name: 600000.5

一个类中三个二级构造函数的 Kotlin 程序——

Java

//main function
fun main(args: Array)
{
    Add(5, 6)
    Add(5, 6, 7)
    Add(5, 6, 7, 8)
}
//class with three secondary constructors
class Add
{
    constructor(a: Int, b: Int)
    {
        var c = a + b
        println("Sum of 5, 6 = ${c}")
    }
    constructor(a: Int, b: Int, c: Int)
    {
        var d = a + b + c
        println("Sum of 5, 6, 7 = ${d}")
    }
    constructor(a: Int, b: Int, c: Int, d: Int)
    {
        var e = a + b + c + d
        println("Sum of 5, 6, 7, 8 = ${e}")
    }
}

输出:

Sum of 5, 6 = 11
Sum of 5, 6, 7 = 18
Sum of 5, 6, 7, 8 = 26

从另一个调用一个辅助构造函数——

辅助构造函数可以使用this()函数调用同一类的另一个辅助构造函数。在下面的程序中,我们使用this(a,b,7)调用了另一个构造函数,因为调用该构造函数需要三个参数。
从另一个调用一个构造函数的 Kotlin 程序-

Java

//main function
fun main(args: Array)
{
    Add(5,6)
}
class Add {
    // calling another secondary using this
    constructor(a: Int,b:Int) : this(a,b,7) {
        var sumOfTwo = a + b
        println("The sum of two numbers 5 and 6 is: $sumOfTwo")
    }
    // this executes first
    constructor(a: Int, b: Int,c: Int) {
        var sumOfThree = a + b + c
        println("The sum of three numbers 5,6 and 7 is: $sumOfThree")
    }
}

输出:

The sum of three numbers 5,6 and 7 is: 18
The sum of two numbers 5 and 6 is: 11

从子类二级构造函数调用父类二级构造函数——

我们可以使用super关键字从子类调用父类的二级构造函数。在下面的程序中,我们展示了调用的过程。

Java

fun main(args: Array) {
    Child(18018, "Sagnik")
}
open class Parent {
    constructor (emp_id: Int, emp_name: String, emp_salary: Double) {
        var id: Int = emp_id
        var name: String = emp_name
        var salary : Double = emp_salary
        println("Employee id is: $id")
        println("Employee name: $name")
        println("Employee salary: $salary")
        println()
    }
}
class Child : Parent {
    constructor (emp_id : Int, emp_name: String):super(emp_id,emp_name,500000.55){
        var id: Int = emp_id
        var name: String = emp_name
        println("Employee id is: $id")
        println("Employee name: $name")
    }
}

输出:

Employee id is: 18018
Employee name: Sagnik
Employee salary: 500000.55

Employee id is: 18018
Employee name: Sagnik