是否可以从嵌套类引用外部类指针?

tad*_*ole 0 java this

我需要this在内部类中使用外部类的指针。

我不知道如何在不保存this指针的情况下做到这一点。还有其他选择吗?

class outerclass {

  outerClass thisPointer;

  outerclass () {
      //
      // NOTE: I am saving this pointer here to reference
      // by the inner class later.   I am trying to find
      // a different alternative instead of saving this pointer.
      //
      thisPointer = this; 
  }

  class innerClass {

      void doSomething () {

           //
           // is there a way to reference the outter class
           // without having to save the thisPointer from
           // the outter class.
           // NOTE someObject is a class outside of the
           // outterclass control.
           //
           someObject.someMethod (thisPointer);
      }
  }     
}  
Run Code Online (Sandbox Code Playgroud)

pb2*_*b2q 6

使用语法NameOfOuterClass.this

void doSomething () {
    someObject.someMethod(outerClass.this);
}
Run Code Online (Sandbox Code Playgroud)