- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 123字
- 2021-06-24 14:13:28
This expression
When inside a class or function, we often want to refer to the enclosing instance. For example, an instance may want to invoke a method passing itself as an argument. To do this, we use the this keyword:
class Person(name: String) { fun printMe() = println(this) }
In Kotlin terminology, the reference referred to by the this keyword is called the current receiver. This is because it was the instance that received the invocation of the function. For example, if we have a string and invoke the length, the string instance is the receiver.
In members of a class, this refers to the class instance. In extension functions, this refers to the instance that the extension function was applied to.
推薦閱讀
- Mastering NetBeans
- Learning NServiceBus(Second Edition)
- OpenDaylight Cookbook
- ASP.NET Core 5.0開發入門與實戰
- Python自動化運維快速入門(第2版)
- Essential Angular
- PhoneGap Mobile Application Development Cookbook
- 從Java到Web程序設計教程
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Mastering Leap Motion
- 嵌入式C編程實戰
- Node.js Web Development
- Switching to Angular 2
- 陪孩子像搭積木一樣學編程:Python真好玩+Scratch趣味編程(全2冊)
- 深入理解C++11:C++11新特性解析與應用