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

  • Functional Kotlin
  • Mario Arias Rivu Chakraborty
  • 97字
  • 2021-06-24 19:15:24

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.

主站蜘蛛池模板: 中山市| 离岛区| 仲巴县| 赣榆县| 沿河| 德州市| 卢龙县| 浦城县| 若尔盖县| 永吉县| 九江县| 濉溪县| 黄大仙区| 渝中区| 新乡市| 儋州市| 北票市| 海安县| 临邑县| 门头沟区| 漳浦县| 冀州市| 武城县| 乐昌市| 青龙| 民勤县| 嘉义市| 中方县| 怀宁县| 桐乡市| 绥棱县| 即墨市| 三门峡市| 南漳县| 安化县| 威宁| 贵阳市| 芜湖市| 麦盖提县| 西青区| 南皮县|