我可以从内部类访问“secondary this”吗?

时间:2016-08-23 17:57:46

标签: java nested inner-classes derived-class

我有基本课程

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

1 个答案:

答案 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嵌套类,因为它们不属于外部类的实例。