Created
October 23, 2023 12:58
-
-
Save sasssass/2051e95cc73525ad23a6d19326a73457 to your computer and use it in GitHub Desktop.
encapsulation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Something { | |
var number: Int = 0 | |
get() { | |
println("someone is trying to read number") | |
return field | |
} | |
set(input: Int) { | |
println("someone is trying to change number") | |
if (input >= 0) | |
field = input | |
} | |
} | |
fun main() { | |
val obj : Something = Something() | |
obj.number = -1 // won't change the number | |
obj.number = 2 // number will be 2 | |
println(obj.number) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment