面向对象
类
声明类
class A {
// ....
}
// 如果是一个空的类,可以省略大括号
class B构造函数
主构造函数
class Person constructor(firstName: String) {}class Person(firstName: String) { }class Customer public @Inject constructor(name: String) { /*……*/ }class InitOrderDemo(name: String) { val firstProperty = "First property: $name".also(::println) init { println("First initializer block that prints ${name}") } val secondProperty = "Second property: ${name.length}".also(::println) init { println("Second initializer block that prints ${name.length}") } } // First property: hello // First initializer block that prints hello // Second property: 5 // Second initializer block that prints 5class Customer(name: String) { val customerKey = name.toUpperCase() }class Person( val firstName: String, val lastName: String, var age: Int, // trailing comma ) { // ... }class Person(firstName: String) { } // 等同于 class Person public constructor (firstName: String) { }class Customer(val customerName: String = "")
次构造函数
创建类实例
最后更新于