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

  • 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
)
主站蜘蛛池模板: 潜江市| 饶河县| 闵行区| 荆门市| 高要市| 九龙坡区| 涪陵区| 广饶县| 历史| 炉霍县| 丹棱县| 古田县| 富顺县| 福建省| 曲周县| 工布江达县| 谢通门县| 盐山县| 沂南县| 那坡县| 合水县| 建平县| 汕尾市| 永城市| 南澳县| 西宁市| 嘉义县| 民丰县| 辽宁省| 临武县| 綦江县| 丹江口市| 桂东县| 惠州市| 上虞市| 江永县| 南召县| 乐昌市| 礼泉县| 中江县| 苍南县|