国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩

當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > java中的匿名內(nèi)部類總結(jié)

java中的匿名內(nèi)部類總結(jié) 時(shí)間:2014-06-10      來源:未知

匿名內(nèi)部類也就是沒有名字的內(nèi)部類

正因?yàn)闆]有名字,所以匿名內(nèi)部類只能使用一次,它通常用來簡(jiǎn)化代碼編寫

但使用匿名內(nèi)部類還有個(gè)前提條件:必須繼承一個(gè)父類或?qū)崿F(xiàn)一個(gè)接口

實(shí)例1:不使用匿名內(nèi)部類來實(shí)現(xiàn)抽象方法

abstract class Person { 
               public abstract void eat(); 
        } 
        
        class Child extends Person { 
               public void eat() { 
                      System.out.println("eat something"); 
               } 
        } 
        
        public class Demo { 
               public static void main(String[] args) { 
                      Person p = new Child(); 
                      p.eat(); 
               } 
        }

運(yùn)行結(jié)果:eat something

可以看到,我們用Child繼承了Person類,然后實(shí)現(xiàn)了Child的一個(gè)實(shí)例,將其向上轉(zhuǎn)型為Person類的引用

但是,如果此處的Child類只使用一次,那么將其編寫為獨(dú)立的一個(gè)類豈不是很麻煩?

這個(gè)時(shí)候就引入了匿名內(nèi)部類

實(shí)例2:匿名內(nèi)部類的基本實(shí)現(xiàn)

abstract class Person { 
                public abstract void eat(); 
        } 
        
        public class Demo { 
                public static void main(String[] args) { 
                        Person p = new Person() { 
                                public void eat() { 
                                        System.out.println("eat something"); 
                                } 
                        }; 
                        p.eat(); 
                } 
        }

運(yùn)行結(jié)果:eat something

可以看到,我們直接將抽象類Person中的方法在大括號(hào)中實(shí)現(xiàn)了

這樣便可以省略一個(gè)類的書寫

并且,匿名內(nèi)部類還能用于接口上

實(shí)例3:在接口上使用匿名內(nèi)部類

interface Person { 
                public void eat(); 
        } 
        
        public class Demo { 
                public static void main(String[] args) { 
                        Person p = new Person() { 
                                public void eat() { 
                                        System.out.println("eat something"); 
                                } 
                        }; 
                        p.eat(); 
                } 
        }

運(yùn)行結(jié)果:eat something

由上面的例子可以看出,只要一個(gè)類是抽象的或是一個(gè)接口,那么其子類中的方法都可以使用匿名內(nèi)部類來實(shí)現(xiàn)

常用的情況就是在多線程的實(shí)現(xiàn)上,因?yàn)橐獙?shí)現(xiàn)多線程必須繼承Thread類或是繼承Runnable接口

實(shí)例4:Thread類的匿名內(nèi)部類實(shí)現(xiàn)

public class Demo { 
                public static void main(String[] args) { 
                        Thread t = new Thread() { 
                                public void run() { 
                                        for (int i = 1; i <= 5; i++) { 
                                                System.out.print(i + " "); 
                                        } 
                                } 
                        }; 
                        t.start(); 
                } 
        }

運(yùn)行結(jié)果:1 2 3 4 5

實(shí)例5:Runnable接口的匿名內(nèi)部類實(shí)現(xiàn)

public class Demo { 
                public static void main(String[] args) { 
                        Runnable r = new Runnable() { 
                                public void run() { 
                                        for (int i = 1; i <= 5; i++) { 
                                                System.out.print(i + " "); 
                                        } 
                                } 
                        }; 
                        Thread t = new Thread(r); 
                        t.start(); 
                } 
        }

運(yùn)行結(jié)果:1 2 3 4 5

上一篇:java中的內(nèi)部類分析

下一篇:網(wǎng)絡(luò)編程常用函數(shù)接口

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺(tái)專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號(hào)-5,京公海網(wǎng)安備11010802025203號(hào)

回到頂部