class Person {
var name: String = "John Doe"
get() {
println("Getting the name: $field")
return field
}
set(value) {
println("Setting the name to: $value")
field = value
}
var age: Int = 0
get() {
println("Getting the age: $field")
return field
}
set(value) {
if (value >= 0) {
println("Setting the age to: $value")
field = value
} else {
println("Age cannot be negative")
}
}
}
fun main() {
val person = Person()
person.name = "Alice"
println(person.name)
person.age = 25
println(person.age)
person.age = -5
}