<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    eric-1001c

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      3 隨筆 :: 45 文章 :: 12 評(píng)論 :: 0 Trackbacks

     

    class Egg{
        
    private Yolk y;
        
    protected class Yolk{
            
    public Yolk(){
                System.out.println(
    "Egg.Yolk()");
            }

        }

        
    public Egg(){
            System.out.println(
    "new Egg()");
            y 
    = new Yolk();
        }

    }

    //bigegg extend egg, and try "override" the yolk
    public class BigEgg extends Egg{
        
    public class Yolk{
            
    public Yolk(){
                System.out.println(
    "BigEgg.Ylok()");    
            }

        }

        
    public static void main(String[] args) {
            
    new BigEgg();
        }

    }

    內(nèi)部類是一種非常有用的機(jī)制,它允許你把一些邏輯相關(guān)的類組織在一起,并控制位于內(nèi)部類的可視性(進(jìn)入修飾符和其它普通類元素沒有區(qū)別)。同時(shí)內(nèi)部類能夠與外部類通信。利用內(nèi)部類的特性可以使得寫出的代碼優(yōu)雅而清雅。

    1)內(nèi)部類可以在內(nèi)部進(jìn)行使用,更典型的情況是在外部類設(shè)置一個(gè)方法返回一個(gè)指向內(nèi)部類的引用
    2)如果想從外部類的非靜態(tài)方法之外的任意位置創(chuàng)建某個(gè)內(nèi)部類的對(duì)象(這個(gè)內(nèi)部類必須是public),必須具體指明這個(gè)對(duì)象的類型:OuterClassName.InnerClassName;在new表達(dá)式中提供對(duì)其它外部類對(duì)象的引用,需要使用.new語法。


    OutClass oc 
    = new OutClass();
    OutClass.InnerClass ocic 
    = oc.new InnerClass();

    這個(gè)例子也告訴我們,必須使用外部類的對(duì)象來創(chuàng)建內(nèi)部類對(duì)象,不能直接創(chuàng)建內(nèi)部類對(duì)象,因?yàn)樗鼈冎氨仨毥⒁粋€(gè)聯(lián)系(當(dāng)然除了嵌套類--靜態(tài)內(nèi)部類)

    3)內(nèi)部類和制造它的外圍對(duì)象之間有一種聯(lián)系(對(duì)象創(chuàng)建一個(gè)內(nèi)部類對(duì)象時(shí),內(nèi)部類對(duì)象會(huì)捕獲一個(gè)指向那個(gè)外圍類對(duì)象的引用),這種聯(lián)系使得它能訪問外圍對(duì)象的所有成員,而不要任何特殊條件。此外,內(nèi)部類還擁有其外圍類的所有元素的訪問權(quán)。(是不是可以把內(nèi)部類看成外部類的一個(gè)元素,一個(gè)“特殊”的“類元素”)
    如果需要生成對(duì)外部類對(duì)象的引用,可以使用外部類的名字后面緊跟圓點(diǎn)和this“.this”:

    public class Outer{

       
    public class Inner{
       
          
    public Outer getOuter(){
             
    return Outer.this;
          }


    }

    4) 當(dāng)將內(nèi)部類向上轉(zhuǎn)型為其基類,尤其是轉(zhuǎn)型為一個(gè)接口的時(shí)候,內(nèi)部類就有了用武之地。這是因?yàn)閮?nèi)部類--某個(gè)接口的實(shí)現(xiàn)--能夠完全不可見,并且不可用(將內(nèi)部類進(jìn)入修飾符設(shè)置為private或protected就可以了)。所得到的只是指向基類或接口的引用,所以能夠很方便地隱藏實(shí)現(xiàn)細(xì)節(jié),也完全阻止任何依賴于類型的編碼
    package chapter10;

    interface Destination{
        String readLabel();
    }

    interface Contents{
        
    int value();
    }

    class OuterInner{
        
    //inner class1
        private class PDestination implements Destination{
            
    private String label;
            
    private PDestination (String whereto){
                label 
    = whereto;
            }

            
    public String readLabel() {
                
    return label;
            }

        }
    ;
        
    //inner class2
        protected class PContents implements Contents{
            
    private int i = 11;
            
    public int value() {
                
    return i;
            }

        }
    ;
        
        
    //method to ge the reference of the inner class pdestination and pcontent
    //    notice that we return a interface instead of the implements of the interface
        public Destination getDestination(String s){
            
    return new PDestination(s);
        }

        
    public Contents getContents(){
            
    return new PContents();
        }

    }


    public class InnerUpcast {

        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            OuterInner oi 
    = new OuterInner();
    //        we also use the interface as the type of the class instead of the inner class type
            Contents c =oi.getContents();
            Destination d 
    = oi.getDestination("Hello");
        }


    5) 同樣的我們可以在任意一個(gè)方法中或作用域內(nèi)創(chuàng)建一個(gè)內(nèi)部類,除了要注意它(內(nèi)部類)的使用作用域外,其它用法和在類的內(nèi)部類用法無異。
    6)匿名內(nèi)部類:沒有名字的,不能有構(gòu)造器的類
    interface Contents{
        
    int value();
    }

    abstract class Base{
        
    public abstract void f();
    }

    //use anonymous inner class as the vaule of return
    //play your attendtion on that the value of return not only is a inner class
    //but also it will be casted up as a interface, that's the amazing place
    class OuterInner{
    //  use interface as anonymous inner class
        public Contents getContents(){
            
    return new Contents(){
                
    private int i = 11;
                
    public int value() {
                    
    return i;
                }

            }
    ;
        }

    //    use abstract class as anonymous inner class
        public Base getBase(){
            
    return new Base(){
                
    public void f(){
                    
    //do something here
                }

            }
    ;
        }

    }


    public class AnonymousInner {
        
    public static void main(String[] args) {
            OuterInner oi 
    = new OuterInner();
            Contents c 
    =oi.getContents();
            Base b 
    = oi.getBase();
        }

    }
    匿名內(nèi)部類既可以擴(kuò)展類(抽象類),也可以實(shí)現(xiàn)接口,但不能兩者兼?zhèn)洹H绻涿麅?nèi)部類要使用在其外部定義的對(duì)象,則要求該參數(shù)引用為final

    7)要?jiǎng)?chuàng)建嵌套類(靜態(tài)內(nèi)部類)意味著:并不需要其外圍類的對(duì)象;不能從嵌套類的對(duì)象中訪問非靜態(tài)的外圍類對(duì)象。嵌套類與普通內(nèi)部類還有一個(gè)區(qū)別:普通內(nèi)部類的字段與方法只能放在類的外部層次上,所以普通的內(nèi)部類不能有static數(shù)據(jù)和static字段,也不能包含嵌套類。但是嵌套類可以包含所有這些東西。
    8)一個(gè)內(nèi)部類被嵌套多少層并不重要,它能透明地訪問所有它所嵌入的外圍類的所有成員
    9)“每個(gè)內(nèi)部類都能獨(dú)立地繼承自一個(gè)(接口的)實(shí)現(xiàn),所以無論外圍類是否已經(jīng)繼承了某個(gè)(接口的)實(shí)現(xiàn),對(duì)于內(nèi)部類都沒有影響”,這樣的內(nèi)部類特性使得其很多解決“多重繼承”:
           a. 內(nèi)部類可以有多個(gè)實(shí)例,每個(gè)實(shí)例都有自己的狀態(tài)信息,并且與其外圍類對(duì)象的信息相互獨(dú)立(通過匿名內(nèi)部類實(shí)現(xiàn))
           b. 在單個(gè)外圍類中,可以讓多個(gè)內(nèi)部類以不同的方式實(shí)現(xiàn)同一個(gè)接口,或繼承同一個(gè)類(通過匿名內(nèi)部類實(shí)現(xiàn))
           c. 創(chuàng)建內(nèi)部類對(duì)象的時(shí)候并不依賴于外圍類對(duì)象的創(chuàng)建,并沒有令人迷惑的“is-a”關(guān)系,它就是一個(gè)獨(dú)立的實(shí)體

    class PetSequence{
        String[] str 
    =new String[]{"Rat","Manx","Cymric","Mutt","Pug"};
        
    protected ArrayList<String> pets = new ArrayList<String>(
                Arrays.asList(str));
    }


    public class NonClollectionSequence extends PetSequence{
    //    我們在這創(chuàng)建了3個(gè)方法,返回類型都是Iterable接口。但不同方法對(duì)Iterable接口的實(shí)現(xiàn)是不同的
    //    使用匿名內(nèi)部類很好的以不同的方式實(shí)現(xiàn)同一個(gè)接口,而且并不依賴于外圍對(duì)象
        public Iterable<String> Iterator(){
            
    return new Iterable<String>(){
                
    public Iterator<String> iterator(){
                    
    return new Iterator<String>(){
                        
    private int index = 0;
                        
    public boolean hasNext(){
                            
    return index< pets.size();
                        }

                        
    public String next(){return pets.get(index++);}
                        
    public void remove(){
                            
    throw new UnsupportedOperationException();
                        }

                    }
    ;
                }

            }
    ;
        }

        
        
    public Iterable<String> reversed(){
            
    return new Iterable<String>(){
                
    public Iterator<String> iterator(){
                    
    return new Iterator<String>(){
                        
    private int index = pets.size()-1;
                        
    public boolean hasNext(){
                            
    return index< pets.size();
                        }

                        
    public String next(){
                            
    return pets.get(index--);
                        }

                        
    public void remove(){
                            
    throw new UnsupportedOperationException();
                        }

                    }
    ;
                }

            }
    ;
        }

        
        
    public Iterable<String> randomized(){
            
    return new Iterable<String>(){
                
    public Iterator<String> iterator(){
                    List
    <String> shuffed = new ArrayList<String>(Arrays.asList(str));
                    Collections.shuffle(shuffed, 
    new Random(47));
                    
    return shuffed.iterator();
                }

            }
    ;
        }


        
    public static void main(String[] args) {
            NonClollectionSequence s 
    = new NonClollectionSequence();
            
    for(String string: s.reversed())
                System.out.println(string);
            System.out.println();
            
    for(String string2: s.randomized())
                System.out.println(string2);
        }

    }

    10)如果創(chuàng)建一個(gè)繼承具有內(nèi)部類的外圍類的新類,并在新類中試圖覆蓋內(nèi)部類,會(huì)發(fā)生什么呢?

    當(dāng)繼承了某個(gè)外圍類的時(shí)候,內(nèi)部類并沒有發(fā)生什么特別的神奇的變化。這兩個(gè)內(nèi)部類是完全獨(dú)立的兩個(gè)實(shí)體,各自在自己的命名空間內(nèi)
    11)內(nèi)部類的標(biāo)識(shí)符:外圍類的名字,加上‘$’,再加上內(nèi)部類的名字,比如OuterClass$InnerClass;對(duì)了匿名內(nèi)部類,編譯器會(huì)簡單地產(chǎn)生一個(gè)數(shù)字作為其標(biāo)識(shí)符,比如OuterClass$1
    12)局部內(nèi)部類VS匿名內(nèi)部類
          a. 局部內(nèi)部類可以提供一個(gè)構(gòu)造器,方便重載構(gòu)造器,而匿名內(nèi)部類只能用于實(shí)例初始化
          b. 局部內(nèi)部類可以被使用多次,然而匿名內(nèi)部類只能實(shí)例化一次
    posted on 2007-07-17 11:18 Eric-1001c 閱讀(229) 評(píng)論(0)  編輯  收藏 所屬分類: ThinkingInJava

    只有注冊用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 一级毛片aaaaaa免费看| a级毛片无码免费真人久久| 希望影院高清免费观看视频| 亚洲国产精品久久久天堂| 美女18毛片免费视频| 四虎影永久在线高清免费| 久久精品国产亚洲AV电影网| 午夜免费福利在线| 日韩欧美亚洲中文乱码| 国产成人精品高清免费| 一道本在线免费视频| 亚洲中文字幕无码不卡电影| 两个人www免费高清视频| 亚洲AV日韩AV鸥美在线观看| 久久精品免费观看国产| 亚洲一区二区三区四区在线观看| 99精品视频在线观看免费专区| 亚洲视频国产精品| 一个人免费观看视频www | 91精品国产亚洲爽啪在线影院 | 亚洲高清中文字幕免费| 亚洲熟妇AV一区二区三区浪潮| 国产一级淫片免费播放| 中文字幕不卡免费视频| 精品亚洲成a人片在线观看| 成人片黄网站A毛片免费| 香蕉97碰碰视频免费| 亚洲人成网7777777国产| 亚洲黄色三级视频| 亚洲免费网站观看视频| 国产91成人精品亚洲精品| 亚洲精品午夜无码电影网| 最新黄色免费网站| 狼人大香伊蕉国产WWW亚洲| 亚洲乱码国产乱码精品精| 每天更新的免费av片在线观看| 亚洲AV无码成人网站在线观看| 亚洲香蕉成人AV网站在线观看| 最近中文字幕免费2019| 国产成人亚洲综合在线| 久久亚洲精品成人av无码网站|