- 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.
推薦閱讀
- 一步一步學(xué)Spring Boot 2:微服務(wù)項(xiàng)目實(shí)戰(zhàn)
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)
- Python程序設(shè)計(jì)(第3版)
- Implementing Cisco Networking Solutions
- Mastering Scientific Computing with R
- Java設(shè)計(jì)模式及實(shí)踐
- Oracle BAM 11gR1 Handbook
- SSM輕量級(jí)框架應(yīng)用實(shí)戰(zhàn)
- Java程序設(shè)計(jì)案例教程
- Rust游戲開發(fā)實(shí)戰(zhàn)
- Scala編程(第5版)
- Instant Automapper
- Ext JS 4 Plugin and Extension Development
- HTML5游戲開發(fā)實(shí)戰(zhàn)
- Implementing DevOps with Ansible 2