官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 湾仔区| 兴义市| 普洱| 恩施市| 邻水| 桐城市| 云霄县| 金秀| 瑞丽市| 昌图县| 宁晋县| 伽师县| 蓬溪县| 柳河县| 榕江县| 阿拉善右旗| 景宁| 聂荣县| 乌兰浩特市| 平顶山市| 鄂伦春自治旗| 建阳市| 巴塘县| 疏附县| 报价| 安福县| 德钦县| 疏勒县| 隆德县| 介休市| 漳浦县| 永康市| 保定市| 秀山| 阿拉善右旗| 天镇县| 历史| 工布江达县| 贺州市| 上思县| 三门峡市|