Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
S_A_DataClasses.kt
// Example of data class (saved more than 10 lines in comparison to Java)
data class User(val name: String, val surname: String, val age: Int)
// Example of custom getters and setters on a property
class MrUser {
var name: String = ""
get() {
return "Mr. $field" // Get name with "Mr." prefix
}
set(newValue) {
println("old: '$field', new: '$newValue'") // Print change
field = newValue // Store new value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment