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

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.

主站蜘蛛池模板: 龙川县| 石棉县| 万载县| 汶上县| 南丰县| 安宁市| 北宁市| 盐边县| 巢湖市| 洛川县| 武安市| 长泰县| 蒲江县| 龙州县| 吉隆县| 呼伦贝尔市| 平凉市| 郎溪县| 漯河市| 永靖县| 平利县| 循化| 肥乡县| 石林| 石河子市| 乌兰县| 罗甸县| 社旗县| 左权县| 贺兰县| 黄石市| 厦门市| 门头沟区| 阿克苏市| 晋江市| 晋州市| 屏东县| 祁连县| 镇江市| 城市| 赤壁市|