1.定义类
package classStudy
class 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 classStudy
class 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 classStudy
import kotlin.math.absoluteValue
class Player1(
//这是主构造函数
_name:String,
_age:Int,
_isNormal:Boolean,
_id:String
) {
var id = _id
get() = field.capitalize()
set(value) {
field = value.trim()
}
var name = _name
get() = field.capitalize()
private set(value) {
field =value.trim()
}
var age = _age
get() = age.absoluteValue
set(value) {
field=age.absoluteValue
}
var isNormal = _isNormal
}
fun main() {
val player1 = Player1("Jack", 20, true, "hjmj")
player1.id="s"
}
4.主构造函数定义属性
package classStudy
import kotlin.math.absoluteValue
class Player2(
//这是主构造函数
_name:String,
var age:Int,
var isNormal:Boolean,
var id:String
) {
var name = _name
get() = field.capitalize()
set(value) {
field =value.trim()
}
}
fun main() {
val player1 = Player1("Jack", 20, true, "hjmj")
player1.id="s"
}
5.次构造函数
package classStudy
import kotlin.math.absoluteValue
class Player2(
//这是主构造函数
_name: String,
var age: Int,
var isNormal: Boolean,
var id: String
) {
var name = _name
get() = 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 classStudy
import kotlin.math.absoluteValue
class Player2(
//这是主构造函数
_name: String,
var age: Int = 20,
var isNormal: Boolean,
var id: String
) {
var name = _name
get() = 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 classStudy
import kotlin.math.absoluteValue
class Player2(
//这是主构造函数
_name: String,
var age: Int = 20,
var isNormal: Boolean,
var id: String
) {
var name = _name
get() = 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 classStudy
class Student(
_name:String,
val age:Int // 顺序 1
) {
var name = _name
var score = 10 // 顺序 2
private val hobby = "music"
val subject:String
init {
// 顺序 3
println("initializing student")
subject = "math"
}
constructor(_name:String):this(_name, 10){
// 顺序 4
score = 20
}
}
fun main() {
Student("Jack")
}
9.延迟初始化lateinit
package classStudy
class Player4 {
lateinit var equipment:String
fun ready(){
equipment = "sharp knife"
}
fun battle(){
if (::equipment.isInitialized) println(equipment)
}
}
fun main() {
val player4 = Player4()
// player4.ready()
player4.battle()
}
10.惰性初始化
package classStudy
class Player5(
_name:String
){
var name = _name
val config by lazy { loadConfig() }
private fun loadConfig():String{
println("loading...")
return "xxx"
}
}
fun main() {
val p = Player5("Jack")
Thread.sleep(3000)
println(p.config)
}