Kotlin基本语法 3 类

1.定义类

Kotlin 复制代码
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 复制代码
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 复制代码
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 复制代码
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 复制代码
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 复制代码
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 复制代码
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.初始化顺序

Kotlin 复制代码
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

Kotlin 复制代码
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 复制代码
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方法调用

11.2 陷阱2 顺序问题

11.3 陷阱3 顺序问题

相关推荐
也无晴也无风雨34 分钟前
深入剖析输入URL按下回车,浏览器做了什么
前端·后端·计算机网络
Martin -Tang1 小时前
Vue 3 中,ref 和 reactive的区别
前端·javascript·vue.js
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
FakeOccupational3 小时前
nodejs 020: React语法规则 props和state
前端·javascript·react.js
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
放逐者-保持本心,方可放逐3 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
曹天骄4 小时前
next中服务端组件共享接口数据
前端·javascript·react.js
一丝晨光4 小时前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
阮少年、4 小时前
java后台生成模拟聊天截图并返回给前端
java·开发语言·前端