- 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.
推薦閱讀
- PHP動態網站程序設計
- Web前端開發技術:HTML、CSS、JavaScript(第3版)
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- FreeSWITCH 1.8
- Mastering Ubuntu Server
- 實戰低代碼
- Windows Presentation Foundation Development Cookbook
- 從學徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- 從0到1:Python數據分析
- Unity&VR游戲美術設計實戰
- Android移動應用開發項目教程
- Practical Predictive Analytics
- Google Adsense優化實戰
- Responsive Web Design with jQuery