【发布时间】:2017-08-18 22:32:55
【问题描述】:
-non-static- 内部类可以完全访问外部类的所有常规成员。但是还有另一种方法可以使用 (outerClass.this.regularMember).. 访问这些成员,请查看以下代码:
public class Car {
int x = 3;
public static int hp =6;
public void move()
{
System.out.println(hp);
}
public class Engine{
public int hp =5;
public int capacity;
public void start()
{
Car.this.move(); // or we can use only move();
}
}
}
- 能否解释一下使用
Car.this.move();和move();之间是否存在任何实际区别? - 你如何解释这种 Car.this. 的语法,因为据我了解 this 这里指的是 Type Engine 的对象 但是,如果我使用另一个参考,例如
Engine smallEngine = new Engine();then this 关键字不能被 smallEngine 替代。
【问题讨论】:
标签: java this inner-classes outer-classes