如何在 Kotlin 中的 lambda 表达式中获取类实例?

Dor*_*Vak 4 java lambda inner-classes kotlin

在java中,当我想从内部类获取外部类实例时,我正在编写OutClass.this。在 Kotlin 中,如果 lambda 与接收器一起运行,那么在 lambda 表达式内部是否有办法获取写入 lambda 表达式的类的实例?假设我们有以下代码:

class Dog
{
    fun funcA () : Unit
    {
        println("Dog funcA")
    }
}  
class OuterClass
{ 
   var d : Dog = Dog()
   fun funcA () : Unit
   {
        println("OuterClass funcA")
   }
   
  fun funcWithLambda() : Unit
  {
      d.apply(){
         //place where I want to call OutClass method *funcA*
         // writing *this* will refer to *Dog* instance
         }
  }
}
Run Code Online (Sandbox Code Playgroud)

Lou*_*man 6

您正在寻找this@OuterClass.funcA()