- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 85字
- 2021-06-24 19:15:22
Object declarations
An object can also have a name. This kind of object is called an object declaration:
object Oven {
fun process(product: Bakeable) {
println(product.bake())
}
}
fun main(args: Array<String>) {
val myAlmondCupcake = Cupcake("Almond")
Oven.process(myAlmondCupcake)
}
Objects are singletons; you don't need to instantiate Oven to use it. Objects also can extend other types:
interface Oven {
fun process(product: Bakeable)
}
object ElectricOven: Oven {
override fun process(product: Bakeable) {
println(product.bake())
}
}
fun main(args: Array<String>) {
val myAlmondCupcake = Cupcake("Almond")
ElectricOven.process(myAlmondCupcake)
}
推薦閱讀
- Extending Jenkins
- C++案例趣學
- Kotlin開發教程(全2冊)
- Spring Security Essentials
- Building Serverless Architectures
- Beginning C++ Game Programming
- 網絡數據采集技術:Java網絡爬蟲實戰
- JSP程序設計與案例實戰(慕課版)
- Java高級程序設計
- Manage Your SAP Projects with SAP Activate
- Web程序設計與架構
- Python深度學習:基于PyTorch
- Perl 6 Deep Dive
- Learning Ext JS(Fourth Edition)
- HTML+CSS+JavaScript前端開發(慕課版)