构造器的主要目的是初始化类的属性。在创建类的对象时调用构造器。在 Kotlin 中,我们有两种类型的构造器 - 主构造器和次要构造器。在本指南中,我们将通过示例学习主要和次要构造器,我们还将了解初始化程序块。
Kotlin 中的构造器类型

- 主构造器 - 初始化类
的属性 - 次要构造器 - 初始化类的属性,我们可以在次要构造器中有额外的初始化代码。
1.主构造器
主构造器是初始化类的最简单方法。它被声明为类头的一部分。在下面的示例中,我们将构造器(val name: String, var age: Int)声明为类头的一部分。这是我们的主要构造器,它初始化类Student的name和age属性(数据成员)。
fun main(args: Array<String>) {//creating the object of class Studentval stu = Student("Chaitanya", 31)println("Student Name: ${stu.name}")println("Student Age: ${stu.age}")}class Student(val name: String, var age: Int) {//This is my class. For now I am leaving it empty}
输出:

Kotlin 构造器中的默认值
我们也可以像在下面的例子中那样在 Kotlin 构造器中指定默认值。在这里,我们将默认学生姓名指定为"Student",默认学生年龄为 99。
我们创建了这个类的三个对象,一个具有名称和年龄,第二个对象只有名称,第三个对象没有名称和年龄。正如您在输出中所看到的,创建类的对象时默认值被传递的值覆盖。
fun main(args: Array<String>) {//creating the object of class Studentval stu = Student("Chaitanya", 31)val stu2 = Student("Chaitanya")val stu3 = Student()println("Name: ${stu.name} and Age: ${stu.age}")println("Name: ${stu2.name} and Age: ${stu2.age}")println("Name: ${stu3.name} and Age: ${stu3.age}")}class Student(val name: String = "Student", var age: Int = 99) {//This is my class. For now I am leaving it empty}
输出:

Kotlin 构造器中的初始化程序块
让我们学习在类的构造器中有额外的初始化代码。在下面的例子中,我们有一个初始化块,我们在构造器中使用init声明了它。在这个块中,我们可以有额外的初始化逻辑。
fun main(args: Array<String>) {val stu = Student("Chaitanya", 31)val stu2 = Student("Chaitanya")val stu3 = Student()}class Student(val name: String = "Student", var age: Int = 99) {val stuName: Stringvar stuAge: Intinit{if(name == "Student") {stuName = "NA"stuAge = 0}else {stuName = name.toUpperCase()stuAge = age}println("Student Name is: $stuName")println("Student Age is: $stuAge")}}
输出:

2. Kotlin 次要构造器
Kotlin 中的次要构造器是使用constructor关键字创建的。他们在继承中发挥着重要作用。我建议您在通过次要构造器之前先阅读继承主题。
次要构造器的语法
class Student {constructor(name: String) {// code inside constructor}constructor(name: String, age: Int) {// code inside constructor}}
次要构造器的示例
这是如何在类中声明次要构造器的简单示例。
fun main(args: Array<String>){val obj = Student ("Ajeet", 30)}class Student{constructor(name: String, age: Int){println("Student Name: ${name.toUpperCase()}")println("Student Age: $age")}}
输出:
Student Name: AJEETStudent Age: 30
从另一个调用一个次要构造器
在这个例子中,我们有两个次要构造器,一个具有一个参数,另一个具有两个参数。我们使用this关键字从另一个构造器调用构造器。
fun main(args: Array<String>){val obj = Student ("Ajeet")}class Student{constructor(name: String): this(name, 0){println("secondary constructor with one param")}constructor(name: String, age: Int){println("secondary constructor with two param")println("Student Name: ${name.toUpperCase()}")println("Student Age: $age")}}
输出:

带有父类和子类的 Kotlin 次要构造器示例
在下面的例子中,我们有两个类College,它是父类和子类Student。这里子类的次要构造器使用super关键字调用父类的次要构造器。
fun main(args: Array<String>){val stu = Student("Harry", 24)}open class College{constructor(name: String, age: Int){println("parent class constructor")println("Student Name: ${name.toUpperCase()}")println("Student Age: $age")}}class Student: College{constructor(name: String, age: Int): super(name,age){println("child class constructor")println("Student Name: $name")println("Student Age: $age")}}
输出:
parent class constructorStudent Name: HARRYStudent Age: 24child class constructorStudent Name: HarryStudent Age: 24
