我需要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)
使用语法NameOfOuterClass.this:
void doSomething () {
someObject.someMethod(outerClass.this);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
261 次 |
| 最近记录: |