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

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.

主站蜘蛛池模板: 嘉定区| 德令哈市| 芦山县| 凯里市| 闻喜县| 安新县| 松桃| 疏附县| 阿勒泰市| 海丰县| 五莲县| 宿松县| 栾城县| 桂平市| 普兰店市| 塘沽区| 涪陵区| 怀仁县| 新建县| 保山市| 苏州市| 东兴市| 武威市| 额尔古纳市| 长沙市| 德昌县| 奎屯市| 左云县| 伊春市| 汪清县| 宜兴市| 永清县| 东台市| 喀喇沁旗| 无为县| 黄石市| 凌云县| 博罗县| 广州市| 盐城市| 东丽区|