gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:29:39 24 4
gpt4 key购买 nike

我需要在内部类中使用来自外部类的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);
}
}
}

最佳答案

使用语法NameOfOuterClass.this:

void doSomething () {
someObject.someMethod(outerClass.this);
}

关于java - 是否可以从嵌套类引用外部类指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com