class Host {
int x=2;
class Helper {
int x = 7;
}
public static void main(String[] args){
Host ho = new Host();
Helper he = ho.new Helper();
System.out.println(ho.x);
System.out.println(he.x);
}
}
所以在这里我得到了预期的输出
2
7
现在我想问一下,比如说,我想访问ho's xfrom he。
即我想要在这里2通过 Helper 对象打印我的东西he:
System.out.println(???);
我知道这样的东西没有用,我只是想澄清一下我对嵌套类的概念。我认为这应该是可能的,因为 Helper 对象he有点“绑定”到 Host 对象ho。因为he没有ho. 从 Helper 类内部我可以做到System.out.println(Host.this.x);并且它有效。我无法弄清楚如何从 main 内部进行操作。