oop - Java中,關于匿名內部類調用外部類方法的疑惑
問題描述
今天學習了內部類的知識,知道內部類是可以持有外部類的this,從而在內部類中可以使用OuterClass.this.medthod()來引用相應外部類方法。但是我寫出下代碼,可以運行,然而其中的調用邏輯我不是很明白,望賜教!
public class test {public void report(){ System.out.println('I’m invoked!');}public void perform(){ new Speaker().handleAction(new Action(){@Overridepublic void action() { report();//???為什么能調用report??} });}public static void main(String[] args) { new test().perform();//測試代碼} } class Speaker{void handleAction(Action act){ act.action();} } interface Action{void action(); }
其中設計是這樣的,test對象調用perform方法,該方法其中新建一個Speaker匿名類對象,該對象調用了其handleAction方法,該方法的參數是一個Action接口,接口需要重寫action抽象方法。我使用了屬于test的report方法。輸出是正常的。
那么我想知道,test對象的方法中有一個匿名類的局部對象,局部對象參數是一個實現接口的匿名類,為什么在這個匿名類中可以調用report?它持有test.this指針嗎?我理解中,new Speaker().handleAction(new Action(){....這里面的實現邏輯和test.this一點關系都沒有,也沒有必要持有test.this???
問題解答
回答1:public void perform(){ new Speaker().handleAction(new Action(){@Overridepublic void action() { report();//???為什么能調用report??} });}
new Speaker()不是匿名內部類,它有確切的類名Speakernew Action(){}是匿名內部類,會在編譯的時候給它一個類名(我們假定它的類名叫Test$1,你可以打印this.getClass()看看)看出兩者的區別了嗎?匿名內部類多出了個{}。由于new Action(){}是在test對象的作用域里被創建的,所以它的外部類是Test。
匿名內部類也有構造器,而且和普通類的構造器有點不一樣,編譯的時候會在匿名內部類的構造器的參數列表之前再插入一個參數,這個參數是外部類的對象的引用,編譯之后這個類長這樣:
Test$1 implements Action { final T this$0; Test$1(T this$0){this.this$0 = this$0; }@Override public void action() {this$0.report(); }}
new Action(){...}實際上是創建了Test$1,并且通過構造器把test對象引用傳給Test$1
public void perform(){ new Speaker().handleAction(new Test$1(this));}
所以匿名內部類持有外部類的引用,且可以調用外部類的方法
