- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 156字
- 2021-06-24 19:15:21
Classes
Classes are the foundational type in Kotlin. In Kotlin, a class is a template that provides a state, a behavior, and a type to instances (more on that later).
To define a class, only a name is required:
class VeryBasic
VeryBasic is not very useful, but is still a valid Kotlin syntax.
The VeryBasic class doesn't have any state or behavior; nonetheless, you can declare values of type VeryBasic, as shown in the following code:
fun main(args: Array<String>) {
val basic: VeryBasic = VeryBasic()
}
As you can see, the basic value has a VeryBasic type. To express it differently, basic is an instance of VeryBasic.
In Kotlin, types can be inferred; so, the previous example is equivalent to the following code:
fun main(args: Array<String>) {
val basic = VeryBasic()
}
By being a VeryBasic instance, basic has a copy of the VeryBasic type's state and behavior, namely, none. So sad.
推薦閱讀
- 移動UI設計(微課版)
- 編程珠璣(續)
- Python神經網絡項目實戰
- JavaScript前端開發與實例教程(微課視頻版)
- aelf區塊鏈應用架構指南
- CouchDB and PHP Web Development Beginner’s Guide
- EPLAN實戰設計
- HTML5開發精要與實例詳解
- Python Interviews
- Go語言開發實戰(慕課版)
- C指針原理揭秘:基于底層實現機制
- Java EE 7 with GlassFish 4 Application Server
- Mastering VMware Horizon 7(Second Edition)
- Getting Started with RethinkDB
- R語言數據分析從入門到實戰