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

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.

主站蜘蛛池模板: 镇雄县| 绥中县| 内丘县| 天长市| 洞口县| 广宁县| 应城市| 游戏| 天等县| 衡阳县| 桐城市| 玛多县| 安溪县| 五河县| 嘉义市| 白城市| 富蕴县| 铁力市| 吉首市| 原平市| 洪雅县| 溧水县| 白沙| 平舆县| 沧源| 肥城市| 衡阳市| 汤原县| 鹤山市| 股票| 宜丰县| 山阳县| 孝昌县| 玉树县| 卓尼县| 凯里市| 金堂县| 工布江达县| 林甸县| 建德市| 永定县|