posts - 134,comments - 22,trackbacks - 0

          1、protected的類、類屬變量及方法,包內(nèi)的任何類,及包外的那些繼承了此類的子類才能訪問;

          注意:子類如處于不同的包,則相互間不能訪問繼承自父類的方法。

          所有不能訪問的方法都已經(jīng)被注釋:

            1package packageA;   
            2  
            3public class Base {   
            4    public String publicStr = "publicString";   
            5    protected String protectedStr = "protectedString";   
            6    String defaultStr = "defaultString";   
            7    private String privateStr = "privateString";   
            8  
            9    public void print() {   
           10        System.out.println("packageA.Base has access to");   
           11        System.out.println("    " + publicStr);   
           12        System.out.println("    " + protectedStr);   
           13        System.out.println("    " + defaultStr);   
           14        System.out.println("    " + privateStr);   
           15  
           16        Base b = new Base(); // -- other Base instance   
           17        System.out.println("    b." + b.publicStr);   
           18        System.out.println("    b." + b.protectedStr);   
           19        System.out.println("    b." + b.defaultStr);   
           20        System.out.println("    b." + b.privateStr);   
           21    }
             
           22}
             
           23  
           24--------------------------------------------------------------------------------   
           25  
           26package packageA;   
           27  
           28public class SubA extends Base {   
           29    public void print() {   
           30        System.out.println("packageA.SubA has access to");   
           31        System.out.println("    " + publicStr + " (inherited from Base)");   
           32        System.out.println("    " + protectedStr + " (inherited from Base)");   
           33        System.out.println("    " + defaultStr + " (inherited from Base)");   
           34        // -- not accessible - private elements are even not inherited   
           35        // System.out.println(privateStr);   
           36  
           37        Base b = new Base(); // -- other Base instance   
           38        System.out.println("    b." + b.publicStr);   
           39        System.out.println("    b." + b.protectedStr);   
           40        System.out.println("    b." + b.defaultStr);   
           41        // -- not accessible   
           42        // System.out.println(b.privateStr);   
           43    }
             
           44}
             
           45  
           46--------------------------------------------------------------------------------   
           47  
           48package packageA;   
           49  
           50public class AnotherA {   
           51    public void print() {   
           52        System.out.println("packageA.AnotherA has access to");   
           53        Base b = new Base();   
           54        System.out.println("    b." + b.publicStr);   
           55        System.out.println("    b." + b.protectedStr);   
           56        System.out.println("    b." + b.defaultStr);   
           57        // System.out.println(b.privateStr);   
           58    }
             
           59}
             
           60  
           61--------------------------------------------------------------------------------   
           62  
           63package packageB;   
           64import packageA.Base;   
           65  
           66public class SubB extends Base {   
           67    public void print() {   
           68        System.out.println("packageB.SubB has access to");   
           69        System.out.println("    " + publicStr + " (inherited from Base)");   
           70        // -- protectedStr is inherited element -> accessible   
           71        System.out.println("    " + protectedStr + " (inherited from Base)");   
           72        // -- not accessible   
           73        // System.out.println(defaultStr);   
           74        // System.out.println(privateStr);   
           75  
           76        Base b = new Base(); // -- other Base instance   
           77        System.out.println("    b." + b.publicStr);   
           78        // -- protected element, which belongs to other object -> not accessible   
           79        // System.out.println(b.protectedStr);   
           80  
           81        // -- not accessible   
           82        // System.out.println(b.defaultStr);   
           83        // System.out.println(b.privateStr);   
           84    }
             
           85}
             
           86  
           87--------------------------------------------------------------------------------   
           88  
           89package packageB;   
           90import packageA.Base;   
           91  
           92public class AnotherB {   
           93    public void print() {   
           94        System.out.println("packageB.AnotherB has access to");   
           95        Base b = new Base();   
           96        System.out.println("    b." + b.publicStr);   
           97        // -- not accessible   
           98        // System.out.println(b.protectedStr);   
           99        // System.out.println(b.defaultStr);   
          100        // System.out.println(b.privateStr);   
          101    }
             
          102}
             
          103  
          104--------------------------------------------------------------------------------   
          105  
          106import packageA.*;   
          107import packageB.*;   
          108  
          109// -- testing class   
          110public class TestProtection {   
          111    public static void main(String[] args) {   
          112        // -- all classes are public, so class TestProtection   
          113        // -- has access to all of them   
          114        new Base().print();   
          115        new SubA().print();   
          116        new AnotherA().print();   
          117        new SubB().print();   
          118        new AnotherB().print();   
          119    }
             
          120}
            
          121
          posted on 2009-02-21 19:39 何克勤 閱讀(541) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 九龙县| 上犹县| 吉安县| 平潭县| 永吉县| 新乡市| 苗栗市| 新昌县| 大冶市| 信宜市| 青州市| 乐平市| 芜湖市| 宁安市| 涟源市| 固镇县| 天镇县| 唐山市| 林周县| 皋兰县| 林州市| 喀喇沁旗| 区。| 鄂托克旗| 黎平县| 福安市| 务川| 唐河县| 建平县| 五原县| 元朗区| 福州市| 彝良县| 河北区| 上蔡县| 北流市| 博野县| 临城县| 普陀区| 都昌县| 怀远县|