package loaderman.demo class Person { val name: String get() = "name" var v: Int = 0 var value: Int get() = v set(value) { println("设值" + value) v = value } }
package loaderman import loaderman.demo.Person fun main(arg: Array<String>) { var p = Person() println( p.v) p.value=80 println( p.v) }
filed标识符,可以将filed 当作成员变量使用,也就是通过field读写属性值
package loaderman.demo class Person { val name: String get() = "name" var value: Int=0 get() =field set(value) { println("设值" + value) field = value } }
package loaderman import loaderman.demo.Person fun main(arg: Array<String>) { var p = Person() p.value=80 println( p.value) }