Skip to content

Instantly share code, notes, and snippets.

@vodamiro
Created April 11, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vodamiro/05e6d466ac05e60d77c520d518c1ef70 to your computer and use it in GitHub Desktop.
Save vodamiro/05e6d466ac05e60d77c520d518c1ef70 to your computer and use it in GitHub Desktop.
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