Java 簡述匿名內部類的江湖

摘自Thinking  in Java 第四版

思考:Anonymous Inner Class(匿名內部類)是否可以繼承其它類?是否可以實現接口? 

答案:匿名內部類與正規的繼承相比有些受限,因爲匿名內部類既可以繼承類,也可以實現接口,但是不能兩者兼備,實現接口也只能實現一個。


舉例說明什麼是匿名內部類:


class OuterMyTest {

public Test getFun(){
return new Test(){
private int  i = 1;
public int value() {
return i;
}
};

}


public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test =  aTest.getFun();

}
}


class Test {
void getFun(){
System.out.println("Test.getFun()");
}
}


getFun方法將返回值的生成與表示這個返回值的類的定義結合在一起,

這個類是匿名的,沒有名字的。

創建一個繼承自Test的匿名類的對象,通過new表達式返回的引用被自動向上轉型爲對Test的引用。


如果帶參數 怎麼辦?


class OuterMyTest {

public Test getFun(int j){
return new Test(j){
private int  i = 1;
public int value() {
return i;
}
};
}



public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test =  aTest.getFun(0);

}
}


class Test {
public Test(int j) {
System.out.println("Test.Test");
}


void getFun(){
System.out.println("Test.getFun()");
}
}


如果是:


class OuterMyTest {

public Test getFun(int j){
return new Test(){
private int  i = j;
public int value() {
return i;
}
};
}


public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test =  aTest.getFun(0);

}
}


class Test {
public Test(int j) {
System.out.println("Test.Test(int j)");
}


public Test() {
System.out.println("Test.Test()");
}


void getFun(){
System.out.println("Test.getFun()");
}
}


編譯報錯:Cannot refer to the non-final local variable j defined in an enclosing scope


如果定義一個匿名內部類,並且希望它使用一個在其外部定義的對象,那麼編譯器要求其參數引用是final的。


如果被傳遞給匿名類的基類的構造器,它並不會在匿名類內部被直接使用,則不要求j是final的,

如果在匿名類內部使用,就要求j是final的


class OuterMyTest {

public Test getFun( final String dest,final float price){
return new Test(){
private int  cost;
{
cost = Math.round(price);
if(cost>100){
System.out.println(cost);
}
}
private String lable = dest;
public String readLable() {
return lable;
}
};
}


public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test =  aTest.getFun("china", 109.12F);

}
}


class Test {
public Test(int j) {
System.out.println("Test.Test(int j)");
}


public Test() {
System.out.println("Test.Test()");
}
}





發佈了43 篇原創文章 · 獲贊 29 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章