- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 136字
- 2021-06-24 14:13:28
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.
推薦閱讀
- Kibana Essentials
- SoapUI Cookbook
- Podman實戰
- Mastering KnockoutJS
- Python數據結構與算法(視頻教學版)
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Arduino計算機視覺編程
- Advanced UFT 12 for Test Engineers Cookbook
- C++程序設計
- Python Programming for Arduino
- 青少年Python趣味編程
- Web程序設計與架構
- Azure for Architects
- JavaWeb入門經典
- JavaScript Security