先看一个内部类访问外部类属性的例子,代码如下:
public class InnerClassTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TalkingClock tk = new TalkingClock(true);
tk.start();
}
}
class TalkingClock{
private boolean beep;
public TalkingClock(boolean beep){
this.beep = beep;
}
public void start(){
TimePrinter listener = new TimePrinter();
listener.stop();
}
public void show(){
System.out.print(" world");
}
public class TimePrinter {
public void stop(){
if(beep){
System.out.print("Hello");
show();
}
}
}
}
在内部类TimerPrinter的stop方法中通过外部类的Talkingclock的属性beep值来判断并打印出结果
<span style="font-size:18px;">public class TalkingClock$TimePrinter
{
final TalkingClock this$0;
public void stop()
{
if (TalkingClock.access$0(TalkingClock.this))
{
System.out.print("Hello");
this$0.show();
}
}
public TalkingClock$TimePrinter()
{
this$0 = TalkingClock.this;
super();
}
}</span>
我们可以看到,编译器为了引用外部类,生成了一个附加的实例this$0,并生成了默认的构造函数初始化this$0,其中
TalkingClock.this是外部类的引用,但是access$0这个静态方法是哪来的呢,我们看一下Talking.class
<pre name="code" class="java"><span style="font-size:18px;">class TalkingClock{
private boolean beep;
public TalkingClock(boolean);
static boolean access$0(TalkingClock);
public void start();
}</span>
编译器为我们生成了静态方法access$0它的返回值就是beep的值,这样子就解释了为什么在内部类中的beep变成了TalkingClock.access$0(TalkingClock.this)