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

Data classes

Creating classes whose primary purpose is to hold data is a common pattern in Kotlin (is a common pattern in other languages too, think of JSON or Protobuff).

Kotlin has a particular kind of class for this purpose:

data class Item(val product: BakeryGood,
val unitPrice: Double,
val quantity: Int)

To declare data class, there are some restrictions:

  • The primary constructor should have at least one parameter
  • The primary constructor's parameters must be val or var
  • Data classes can't be abstract, open, sealed, or inner

With these restrictions, data classes give a lot of benefits.

主站蜘蛛池模板: 阿拉尔市| 连城县| 荆州市| 都安| 沙河市| 偏关县| 中阳县| 屏东县| 甘孜县| 凤台县| 莆田市| 双牌县| 习水县| 丹东市| 牟定县| 萍乡市| 刚察县| 梅州市| 依兰县| 奈曼旗| 五华县| 广安市| 肇源县| 德令哈市| 奉节县| 富顺县| 武定县| 曲水县| 新平| 宣城市| 修水县| 台州市| 青海省| 乐安县| 孝感市| 遂宁市| 遂溪县| 寿光市| 兴山县| 黎川县| 女性|