Kotlin基本语法 3 类

1.定义类

Kotlin基本语法 3 类_第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.计算属性与防范竞态条件

Kotlin基本语法 3 类_第2张图片

Kotlin基本语法 3 类_第3张图片 

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.对象的初始化

Kotlin基本语法 3 类_第4张图片

 

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.主构造函数定义属性

Kotlin基本语法 3 类_第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()
        }

}

fun main() {

    val player1 = Player1("Jack", 20, true, "hjmj")
    player1.id="s"


}

 5.次构造函数

Kotlin基本语法 3 类_第6张图片

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. 默认参数

Kotlin基本语法 3 类_第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()
    }


}

fun main() {

    val player2 = Player2(_name = "a", isNormal = true, id = "12") //如果此构造函数和 主构造函数冲突了,会调用次构造函数

    println(player2.age)
}

 7.初始化代码块

Kotlin基本语法 3 类_第8张图片

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)
}

 Kotlin基本语法 3 类_第9张图片

8.初始化顺序

Kotlin基本语法 3 类_第10张图片

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")
}

Kotlin基本语法 3 类_第11张图片

9.延迟初始化lateinit 

Kotlin基本语法 3 类_第12张图片

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.惰性初始化

Kotlin基本语法 3 类_第13张图片

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)

}

 11.初始化陷阱

11.1 陷阱1 必须保证先初始化类属性,在使用init方法调用

Kotlin基本语法 3 类_第14张图片

11.2 陷阱2 顺序问题

Kotlin基本语法 3 类_第15张图片

11.3 陷阱3 顺序问题

Kotlin基本语法 3 类_第16张图片 

你可能感兴趣的:(Kotlin,kotlin,前端,开发语言)