官术网_书友最值得收藏!

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
)
主站蜘蛛池模板: 南通市| 化德县| 宁化县| 蚌埠市| 昌吉市| 利川市| 府谷县| 陈巴尔虎旗| 贡嘎县| 深州市| 北碚区| 宕昌县| 广东省| 西吉县| 铜陵市| 敖汉旗| 兴和县| 潼关县| 广安市| 施秉县| 濮阳县| 夹江县| 宝清县| 常山县| 冀州市| 万州区| 乌拉特前旗| 武山县| 兰西县| 城市| 应用必备| 灵川县| 宁强县| 分宜县| 杂多县| 青阳县| 乌兰县| 泰州市| 三原县| 宝应县| 青阳县|