我刚开始学习Kotlin,其中你可以有一个主要构造函数和一个或多个次要构造函数。这个问题听起来很简单,但我找不到答案(我已经阅读了文档中的“构造函数”部分)-为什么?基本上,我试图理解主要和次要之间的区别以及它们在使用上的区别(似乎没有,那么为什么要分离)?
有明显的语法差异。但一个主要的概念上的区别在于,所有的次要构造函数最终都会委托给主构造函数。
我认为这样理解:主构造函数是创建对象的规范接口,而次要构造函数则像是静态助手,用于将其他参数集转换为符合此接口的参数集。*
*请注意,这是个人解释,没有任何官方文档支持!
// How to declare a primary constructor
class Student constructor(
firstName: String,
lastName: String
) {
}
// We can omit constructor keyword if the primary constructor
// does not have any annotations or visibility modifiers
class Student(
firstName: String,
lastName: String
) {
}
fun main() {
val student1 = Student("Helen", "trump")
}
class Student(
firstName: String,
lastName: String
) {
init {
println("Welcome to the student profile")
}
}
class Pizza constructor (
var crustSize: String,
var crustType: String,
val toppings: MutableList<String> = mutableListOf()
) {
// secondary constructor (no-args)
constructor() : this("SMALL", "THIN")
// secondary constructor (2-args)
constructor(crustSize: String, crustType: String) : this(crustSize, crustType, mutableListOf<String>())
override fun toString(): String = "size: ${crustSize}, type: ${crustType}, toppings: ${toppings}"
}
fun main(args: Array<String>) {
val p1 = Pizza()
val p2 = Pizza("LARGE", "THICK")
val p3 = Pizza("MEDIUM", "REGULAR", mutableListOf("CHEESE", "PEPPERONI"))
println(p1)
println(p2)
println(p3)
}
输出:
size: SMALL, type: THIN, toppings: []
size: LARGE, type: THICK, toppings: []
size: MEDIUM, type: REGULAR, toppings: [CHEESE, PEPPERONI]
init {...}
)运行在主构造函数之后并不完全正确。初始化块在主构造函数期间运行,并且可以与属性初始化器交错运行。两者按照它们在类体中出现的顺序运行。详见Classes。 - Silvio MayoloKotlin的主构造函数帮助您编写简洁的代码:
你可以写没有主体的类,例如:data class
,例如:
data class Data(val value:String)
如果构造函数上没有任何注释,则关键字constructor
可以省略。一个负面的例子:
class Foo @Annotation constructor()
它使继承变得简单,例如:
open class Bar(val value: String);
class Primary(value: String, other: String) : Bar(value)
class Secondary : Bar {
constructor(value: String, other: String) : super(value)
}
它可以使用关键字by
进行委托,但是次要构造函数不能使用。
interface Rule {
fun apply(value: String): Int
}
open class Policy(rule: Rule) : Rule by rule;