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)
}
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不是同一个对象了
}
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"
}
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"
}
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)
}
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)
}
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)
}
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")
}
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()
}
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)
}