qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          創建Java內部類的編譯錯誤處理

           在創建非靜態內部類時,經常會遇到“No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing   instance of type *(e.g. x.new A() where x is an instance of *).”這樣的報錯,其實原因只有一點,內部類是依賴于外部類存在的,所以在使用非靜態內部類時,要求先實例化外部類才可以使用內部類。關于非靜態內部類,我們可以把它理解成外部類的成員變量,我們在使用一個類的非靜態成員變量時要求先對類進行實例化,然后通過對象來調用這個類的非靜態成員變量。這里非靜態內部類同外部類的關系,就如同非靜態成員變量同類的關系一樣。所以在使用非靜態內部類時,要求先實例化外部類。

            下面我給出例子來分析一下:

          package com.csc.innerclasstest;
          /**
           *
           * @author csc
           *
           */
          //外部類
          public class OuterClass {

           /**
            * @param args
            */
           public static void main(String[] args) {
            
            InnerClass innerClass = new InnerClass();
            innerClass.say();
            System.out.println("I am in OuterClass!");
           }
           
           //定義一個內部類
           private class InnerClass{
            
            private void say() {
             System.out.println("I am in InnerClass!");
            }
           }

          }

            上面的代碼的第16行將會報出“No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass).”這樣的編譯錯誤。錯誤的原因如上面紅色字體所述。

            解決方法一:將非靜態內部類轉換成靜態內部類,即在上面程序的第21行的“Private”后面加上“Static”即可。

            解決方法二:先實例化外部類,然后通過外部類來調用內部類的構造函數,代碼如下:

          package com.csc.innerclasstest;
          /**
           *
           * @author csc
           *
           */
          //外部類
          public class OuterClass {

           /**
            * @param args
            */
           public static void main(String[] args) {
            
            //實例化外部類
            OuterClass outerClass = new OuterClass();
            //通過外部類引用內部類
            InnerClass innerClass = outerClass.new InnerClass();
            innerClass.say();
            System.out.println("I am in OuterClass!");
           }
           
           //定義一個內部類
           private class InnerClass{
            
            private void say() {
             System.out.println("I am in InnerClass!");
            }
           }

          }

            上面代碼的16行先進行了外部類的實例化,第18行通過外部類來引用內部類,這樣就不會出現“No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass”這個編譯報錯了。

            本文轉載自:http://blog.csdn.net/caoshichao520326/article/details/8961297

          posted on 2013-05-28 10:23 順其自然EVO 閱讀(168) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 沂源县| 来安县| 措勤县| 云和县| 新源县| 龙井市| 山西省| 信宜市| 张家口市| 荆州市| 沾益县| 化德县| 连城县| 托克逊县| 阳城县| 兴安盟| 河间市| 辽宁省| 阿瓦提县| 建瓯市| 白沙| 深圳市| 韶山市| 盖州市| 兴海县| 洛阳市| 博乐市| 茂名市| 邯郸市| 桂林市| 策勒县| 安丘市| 合作市| 宣恩县| 华宁县| 苗栗市| 任丘市| 忻州市| 平遥县| 乌拉特中旗| 宁南县|