我有基本课程
abstract class Unit {
Unit target;
abstract class UnitAI {/*...*/}
}
从这些中,我得出了
class Infantry extends Unit {
class InfantryAI extends UnitAI {/*...*/}
}
类InfantryAI
能否以某种方式获取用于访问其周围成员的辅助(隐式)this
课程Infantry
?
具体来说,它需要确定其周围的类Infantry
是否被其目标定位,如下所示:
if (/*secondary_this.*/target.target == secondary_this)
或通常由另一个Unit
。
答案 0 :(得分:6)
您可以通过添加类名来访问外部 this
:
Infantry.this.target; //"this" of the Infantry class from inside InfantryAI
Unit.this.target; //"this" of the Unit class from inside UnitAI
这不适用于static
嵌套类,因为它们不属于外部类的实例。