什么是匿名內部類什么時侯用

Anonymous inner class

馬克-to-win:有時如此簡單,都沒有必要清清楚楚明確出類名,用一下就完,就用匿名內部類。注意: 下面的new FigureMark_to_win(){。。。。};的語法形式。它和以往的new FigureMark_to_win()不同,現(xiàn)在的這個new的是FigureMark_to_win的子類(否則“我啥也不是”, 應該被打印出來。)。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。



例2.7_0:

class FigureMark_to_win {
    void whoAmI(){
       System.out.println("我啥也不是");  
    }
}
class Triangle extends FigureMark_to_win {
    void whoAmI() {
        System.out.println("三角形!");
    }
}
public class Test {
    public static void main(String[] args) {
        Triangle d = new Triangle();
        d.whoAmI();
        new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒有像上面一樣提出一個Triangle類的概念,主要是如此簡單,都沒有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。}; ,馬克-to-win: 大家注意一下格式和分號。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
        FigureMark_to_win a = new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("長方形!");
            }
        };
        a.whoAmI();
        new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("圓形");
            }
        }.whoAmI();//連指針都省了
    }
}
結果:

三角形!
三角形!
長方形!
圓形




匿名內部類的父類也可以是抽象類或接口。以下給出例子:

什么是匿名內部類什么時侯用

Anonymous inner class

馬克-to-win:有時如此簡單,都沒有必要清清楚楚明確出類名,用一下就完,就用匿名內部類。注意: 下面的new FigureMark_to_win(){。。。。};的語法形式。它和以往的new FigureMark_to_win()不同,現(xiàn)在的這個new的是FigureMark_to_win的子類(否則“我啥也不是”, 應該被打印出來。)。
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。



例2.7_0:

class FigureMark_to_win {
    void whoAmI(){
       System.out.println("我啥也不是");  
    }
}
class Triangle extends FigureMark_to_win {
    void whoAmI() {
        System.out.println("三角形!");
    }
}
public class Test {
    public static void main(String[] args) {
        Triangle d = new Triangle();
        d.whoAmI();
        new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒有像上面一樣提出一個Triangle類的概念,主要是如此簡單,都沒有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。}; ,馬克-to-win: 大家注意一下格式和分號。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
        FigureMark_to_win a = new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("長方形!");
            }
        };
        a.whoAmI();
        new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("圓形");
            }
        }.whoAmI();//連指針都省了
    }
}
結果:

三角形!
三角形!
長方形!
圓形

匿名內部類的父類也可以是抽象類或接口。以下給出例子:




例2.7---本章源碼
abstract class FigureMark_to_win {
    abstract void whoAmI();
}
class Triangle extends FigureMark_to_win {
    void whoAmI() {
        System.out.println("三角形!");
    }
}
public class Test {
    public static void main(String[] args) {
        Triangle d = new Triangle();
        d.whoAmI();
        new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒有像上面一樣提出一個Triangle類的概念,主要是如此簡單,都沒有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。},馬克-to-win: 大家注意一下格式和分號。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
        FigureMark_to_win a = new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("長方形!");
            }
        };
        a.whoAmI();
        new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("圓形");
            }
        }.whoAmI();//連指針都省了
    }
}



result is:
三角形!
三角形!
長方形!
圓形






例2.7---本章源碼
abstract class FigureMark_to_win {
    abstract void whoAmI();
}
class Triangle extends FigureMark_to_win {
    void whoAmI() {
        System.out.println("三角形!");
    }
}
public class Test {
    public static void main(String[] args) {
        Triangle d = new Triangle();
        d.whoAmI();
        new Triangle().whoAmI();//連指針都省了
/* 馬克-to-win:下面這句程序,都沒有像上面一樣提出一個Triangle類的概念,主要是如此簡單,都沒有必要清清楚楚明確出類名,所以就直接 new FigureMark_to_win(){。。。。},馬克-to-win: 大家注意一下格式和分號。 even though the next statemnt new a abstract class which is not
allowed, it implement the method of whoAmI, so it is not the real abstract class instantiation.
*/
        FigureMark_to_win a = new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("長方形!");
            }
        };
        a.whoAmI();
        new FigureMark_to_win() {
            void whoAmI() {
                System.out.println("圓形");
            }
        }.whoAmI();//連指針都省了
    }
}



result is:
三角形!
三角形!
長方形!
圓形