- Functional Kotlin
- Mario Arias Rivu Chakraborty
- 106字
- 2021-06-24 19:15:21
Methods
In Kotlin, a class's behavior is defined by methods. Technically, a method is a member function, so, anything that we learn about functions in the following chapters also applies to the methods:
class Cupcake(val flavour: String) {
fun eat(): String {
return "nom, nom, nom... delicious $flavour cupcake"
}
}
The eat() method returns a String value. Now, let's call the eat() method, as shown in the following code:
fun main(args: Array<String>) {
val myBlueberryCupcake = Cupcake("Blueberry")
println(myBlueberryCupcake.eat())
}
The following expression is the output of the preceding code:

Nothing mind-blowing, but this is our first method. Later on, we'll do more interesting stuff.
推薦閱讀
- Learning Python Web Penetration Testing
- WildFly:New Features
- 基于粒計算模型的圖像處理
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- Beginning Java Data Structures and Algorithms
- 企業級Java EE架構設計精深實踐
- Python零基礎快樂學習之旅(K12實戰訓練)
- Koa開發:入門、進階與實戰
- Visual Basic程序設計教程
- Android NDK Beginner’s Guide
- Learning Apache Kafka(Second Edition)
- 正則表達式經典實例(第2版)
- 實戰Java高并發程序設計(第3版)
- Mastering Linux Network Administration
- Canvas Cookbook