- Hands-On Design Patterns with Kotlin
- Alexey Soshin
- 128字
- 2021-06-25 20:49:25
Data classes
Remember how Kotlin is all about productiveness? One of the most common tasks for Java developers is to create another Plain Old Java Object (POJO). If you're not familiar with POJO, it is basically an object that only has getters, setters, and an implementation of equals or hashCode methods.
This task is so common that Kotlin has it built into the language:
data class User (val username : String, val password : String)
This will generate a class with two getters and no setters (note the val part), which will also implement equals, hashCode, and clone functions in the correct way.
The introduction of data classes is one of the biggest improvements in reducing the amount of boilerplate in the language.