当前位置:网站首页>Kotlin classes and objects

Kotlin classes and objects

2022-07-04 19:41:00 AdleyTales

import java.util.*

//  Class contains constructors 、 Initialize code block 、 function 、 attribute 、 Inner class 、 Object declaration 
//  Non empty properties must be initialized at the time of definition ,kotlin Provides a way to delay initialization , Use  lateinit  Keyword description properties 
class People {
    
    //  The properties of a class can use keywords  var  Declare variable , Otherwise, use read-only keywords  val  Declared immutable 
    var name: String = ""
    var age: Int = 0

    var addr: String = ""
        get() = field.uppercase()
        set(value) {
    
            field = "G+$value"
        }

    constructor(name: String, age: Int, addr: String) {
    
        this.name = name
        this.age = age
        this.addr = addr

        println("constructor------")
    }

    constructor(name: String, age: Int) {
    
        this.name = name
        this.age = age
    }

    init {
    
        println("init ------")
    }

    //  Member functions 
    fun foo() {
    
        println("foo ---")
    }

    fun info() {
    
        println("name: $name, age: $age, addr: $addr")
    }
}

fun main() {
    
    val p = People("adleytales", 19, "Beijing")
    p.foo()

    p.info()

    p.name = "adley"
    p.age = 18
    p.info()
}
原网站

版权声明
本文为[AdleyTales]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041807344634.html