- 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
)
推薦閱讀
- C語言程序設(shè)計案例教程
- JavaScript從入門到精通(微視頻精編版)
- Learn to Create WordPress Themes by Building 5 Projects
- Programming ArcGIS 10.1 with Python Cookbook
- C語言程序設(shè)計
- 深入淺出DPDK
- Linux C編程:一站式學(xué)習(xí)
- 軟件項目管理實用教程
- Java圖像處理:基于OpenCV與JVM
- Python計算機視覺和自然語言處理
- IPython Interactive Computing and Visualization Cookbook
- Python第三方庫開發(fā)應(yīng)用實戰(zhàn)
- Python無監(jiān)督學(xué)習(xí)
- Parallel Programming with Python
- IBM DB2 9.7 Advanced Application Developer Cookbook