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

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.

主站蜘蛛池模板: 汉寿县| 衢州市| 广东省| 台州市| 抚宁县| 务川| 根河市| 宁城县| 肇源县| 乡宁县| 乡宁县| 龙山县| 石狮市| 玉林市| 紫金县| 英德市| 玉环县| 大宁县| 临夏县| 丹寨县| 浦江县| 资源县| 连云港市| 宜宾县| 大同市| 沅江市| 永和县| 拜泉县| 德江县| 斗六市| 鄢陵县| 新竹市| 太仓市| 靖安县| 旌德县| 郑州市| 金沙县| 伽师县| 库伦旗| 台南县| 凉城县|