1.定义类
package classStudyclass Player {var name:String = "jack"get() = field.capitalize()set(value) {field = value.trim()}
}fun main() {val player = Player()println(player.name)player.name = " asdas "println(player.name)}
2.计算属性与防范竞态条件
package classStudyclass Player {var name:String = "jack"get() = field.capitalize()set(value) {field = value.trim()}
}fun main() {val player = Player()println(player.name)player.name = " asdas "println(player.name)val also = player.name.also {println(it.count())}println("===========")println(player.name===also)//false不是同一个对象了}
3.对象的初始化
package classStudyimport kotlin.math.absoluteValueclass Player1(//这是主构造函数_name:String,_age:Int,_isNormal:Boolean,_id:String
) {var id = _idget() = field.capitalize()set(value) {field = value.trim()}var name = _nameget() = field.capitalize()private set(value) {field =value.trim()}var age = _ageget() = age.absoluteValueset(value) {field=age.absoluteValue}var isNormal = _isNormal
}fun main() {val player1 = Player1("Jack", 20, true, "hjmj")player1.id="s"}
4.主构造函数定义属性
package classStudyimport kotlin.math.absoluteValueclass Player2(//这是主构造函数_name:String,var age:Int,var isNormal:Boolean,var id:String
) {var name = _nameget() = field.capitalize()set(value) {field =value.trim()}}fun main() {val player1 = Player1("Jack", 20, true, "hjmj")player1.id="s"}
5.次构造函数
package classStudyimport kotlin.math.absoluteValueclass Player2(//这是主构造函数_name: String,var age: Int,var isNormal: Boolean,var id: String
) {var name = _nameget() = field.capitalize()set(value) {field = value.trim()}constructor(name: String) : this(name, age = 10, isNormal = true, id = "13")constructor(name: String, age: Int) : this(name, age = age, isNormal = true, id = "13") {this.name = name.uppercase()}}fun main() {val player1 = Player1("Jack", 20, true, "hjmj")player1.id = "s"val player2 = Player2("rose")println(player2)val player3 = Player2("bose", 30)println(player3.name)}
6. 默认参数
package classStudyimport kotlin.math.absoluteValueclass Player2(//这是主构造函数_name: String,var age: Int = 20,var isNormal: Boolean,var id: String
) {var name = _nameget() = field.capitalize()set(value) {field = value.trim()}constructor(name: String,isNormal: Boolean,id: String) : this(name, age = 10, isNormal = isNormal, id = id)constructor(name: String, age: Int) : this(name, age = age, isNormal = true, id = "13") {this.name = name.uppercase()}}fun main() {val player2 = Player2(_name = "a", isNormal = true, id = "12") //如果此构造函数和 主构造函数冲突了,会调用次构造函数println(player2.age)
}
7.初始化代码块
package classStudyimport kotlin.math.absoluteValueclass Player2(//这是主构造函数_name: String,var age: Int = 20,var isNormal: Boolean,var id: String
) {var name = _nameget() = field.capitalize()set(value) {field = value.trim()}constructor(name: String,isNormal: Boolean,id: String) : this(name, age = 10, isNormal = isNormal, id = id)constructor(name: String, age: Int) : this(name, age = age, isNormal = true, id = "13") {this.name = name.uppercase()}init {//构建对象的时候执行 在构造器之后执行require(age>0){"age must be positive "}require(name.isNotBlank()){"Player must have a name . "}}}fun main() {val player2 = Player2(_name = "", age = 12 ,isNormal = true, id = "12") //如果此构造函数和 主构造函数冲突了,会调用次构造函数println(player2.age)
}
8.初始化顺序
package classStudyclass Student(_name:String,val age:Int // 顺序 1
) {var name = _namevar score = 10 // 顺序 2private val hobby = "music"val subject:Stringinit {// 顺序 3println("initializing student")subject = "math"}constructor(_name:String):this(_name, 10){// 顺序 4score = 20}}fun main() {Student("Jack")
}
9.延迟初始化lateinit
package classStudyclass Player4 {lateinit var equipment:Stringfun ready(){equipment = "sharp knife"}fun battle(){if (::equipment.isInitialized) println(equipment)}}fun main() {val player4 = Player4()
// player4.ready()player4.battle()
}
10.惰性初始化
package classStudyclass Player5(_name:String
){var name = _nameval config by lazy { loadConfig() }private fun loadConfig():String{println("loading...")return "xxx"}
}fun main() {val p = Player5("Jack")Thread.sleep(3000)println(p.config)}