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

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.

主站蜘蛛池模板: 贵德县| 吕梁市| 亚东县| 武平县| 济源市| 子长县| 石景山区| 吉林省| 西青区| 图木舒克市| 祁阳县| 双牌县| 黄骅市| 岳池县| 洞口县| 苍溪县| 天等县| 金阳县| 苍南县| 馆陶县| 崇信县| 龙山县| 咸丰县| 伊宁市| 九江县| 兴业县| 石景山区| 沁阳市| 东城区| 淮阳县| 望江县| 沙湾县| 姚安县| 佳木斯市| 临澧县| 永靖县| 深州市| 孙吴县| 来安县| 台州市| 应城市|