posts - 134,comments - 22,trackbacks - 0

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

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

          所有不能訪問的方法都已經被注釋:

            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 何克勤 閱讀(543) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 囊谦县| 上蔡县| 东阿县| 扶绥县| 大姚县| 西城区| 府谷县| 霍林郭勒市| 龙山县| 福鼎市| 南靖县| 鲁山县| 平度市| 兴山县| 郁南县| 东方市| 凭祥市| 文登市| 德安县| 吉林省| 安泽县| 湄潭县| 无为县| 佳木斯市| 乌兰浩特市| 余姚市| 微博| 五华县| 藁城市| 文山县| 漳州市| 海南省| 通州区| 芜湖县| 焦作市| 遂平县| 昭平县| 古蔺县| 田东县| 都江堰市| 民勤县|