- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 162字
- 2021-07-02 21:50:17
Data classes
Each time we create a model class, we end up writing the same boilerplate code for:
- Constructor
- Getter-Setter (s)
- hashCode()
- equals()
- toString()
Kotlin introduced the data keyword where the compiler automatically derives the following stuff based upon the parameters of the primary constructor:
- Getter-Setter (s), which are not technically due to data keywords
- equals()/hashCode() pair
- toString()
- componentN() functions corresponding to the properties in their order of declaration
- copy() function
It saves a lot of boilerplate code and makes the model classes look clean and concise.
The Message class is the data structure representing the messages left by a user on the map. It holds the actual message left, the author of the message and location (coordinates) of the message on the map:
/**
* It represents the message shown on the maps
*/
data class Message(
// The message
var content: String,
// Location of the message
var location: Point? = null,
var id: Int? = null
)
推薦閱讀
- The Complete Rust Programming Reference Guide
- Python程序設計教程(第2版)
- Java程序設計與計算思維
- Unity Game Development Scripting
- 可解釋機器學習:模型、方法與實踐
- UML 基礎與 Rose 建模案例(第3版)
- 計算機應用基礎實踐教程
- Express Web Application Development
- 從Java到Web程序設計教程
- 創意UI:Photoshop玩轉APP設計
- Penetration Testing with the Bash shell
- 你真的會寫代碼嗎
- Microsoft HoloLens By Example
- Mastering Bootstrap 4
- Mastering Object:Oriented Python(Second Edition)