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

Scope

In nested scopes, we may wish to refer to an outer instance. To do that, we must qualify the usage of this, and we do that using labels. The label we use is typically the name of the outer class, but there are more complicated rules for functions and closures as discussed in Chapter 5, Higher-Order Functions and Functional Programming.

Consider the following code:

    class Building(val address: String) { 
      inner class Reception(telephone: String) { 
        fun printAddress() = println(this@Building.address) 
      } 
    } 

Note that the print function needed to qualify access to the Building outer instance. This is because this inside the printAddress() function would have referred to the closest containing class, which, in this case, is Reception. Do not worry about the inner keyword—that will be covered in Chapter 3, Object-oriented Programming in Kotlin.

主站蜘蛛池模板: 广南县| 张家川| 车致| 桦甸市| 中宁县| 城口县| 新干县| 辛集市| 乌拉特中旗| 阿合奇县| 黄平县| 杭锦后旗| 鹰潭市| 固安县| 苏尼特右旗| 台南县| 鄂托克前旗| SHOW| 商水县| 湖南省| 桐柏县| 西畴县| 阳山县| 黄龙县| 家居| 陇南市| 镇安县| 莱阳市| 河间市| 嵊州市| 景宁| 岳阳县| 和林格尔县| 彭山县| 新乐市| 大名县| 浮山县| 孟津县| 华坪县| 特克斯县| 宣威市|