- 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.
推薦閱讀
- Reporting with Visual Studio and Crystal Reports
- UML和模式應(yīng)用(原書第3版)
- Objective-C Memory Management Essentials
- Maven Build Customization
- 算法訓(xùn)練營:入門篇(全彩版)
- 零基礎(chǔ)學(xué)MQL:基于EA的自動化交易編程
- Hands-On C++ Game Animation Programming
- 3D少兒游戲編程(原書第2版)
- VMware虛擬化技術(shù)
- Instant Debian:Build a Web Server
- 分布式數(shù)據(jù)庫原理、架構(gòu)與實踐
- Machine Learning for Developers
- 原型設(shè)計:打造成功產(chǎn)品的實用方法及實踐
- Getting Started with JUCE
- 高性能MVVM框架的設(shè)計與實現(xiàn):San