// 1
class Album(
val id: String,
val name: String,
var count: Long = 0,
var age: Int? = null,
)
// 2
class Album2(id: String, name: String, count: Long, age: Int? = null) {
val id = id
var name = name
var count = count
var age = age
}
// 3
class Album3(id: String, name: String, count: Long, age: Int? = null) {
val id: String
var name: String
var count: Long
var age: Int?
init {
this.id = id
this.name = name
this.count = count
this.age = age
}
}
==============
以上三种写法等价的,如果写成第二种,idea 会提示你可以将 id 、name 这些移到构造函数,就是以一种那样。