- 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
)
推薦閱讀
- Web應用系統開發實踐(C#)
- Docker and Kubernetes for Java Developers
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- 技術領導力:程序員如何才能帶團隊
- 基于免疫進化的算法及應用研究
- Gradle for Android
- 數據結構案例教程(C/C++版)
- Android系統級深入開發
- 西門子S7-200 SMART PLC編程從入門到實踐
- 用戶體驗可視化指南
- 零代碼實戰:企業級應用搭建與案例詳解
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- 精通Spring:Java Web開發與Spring Boot高級功能
- Android 游戲開發大全(第二版)
- Learn Linux Quickly