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

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.

主站蜘蛛池模板: 花莲市| 云浮市| 元朗区| 固镇县| 凤山市| 黄浦区| 茌平县| 洮南市| 曲阜市| 新龙县| 武汉市| 陇南市| 黔江区| 呼图壁县| 安康市| 白水县| 黄石市| 沿河| 方正县| 高台县| 奇台县| 房山区| 施甸县| 涞水县| 白玉县| 弋阳县| 凤台县| 上虞市| 清涧县| 札达县| 延寿县| 长沙县| 宁城县| 鸡西市| 新余市| 万源市| 古浪县| 全南县| 马山县| 宿松县| 永清县|