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

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

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

    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 何克勤 閱讀(541) 評論(0)  編輯  收藏 所屬分類: J2SE
    主站蜘蛛池模板: 一级毛片在线免费播放| 国产AV无码专区亚洲AVJULIA| 最近2019中文免费字幕| 久久WWW免费人成一看片| 久久国产乱子伦免费精品| 无码免费一区二区三区免费播放| 国产午夜免费高清久久影院| 免费看一区二区三区四区| 鲁丝片一区二区三区免费| 国产精品99久久免费观看| 在线免费中文字幕| 在线永久免费的视频草莓| 野花高清在线观看免费3中文 | 一级特黄aaa大片免费看| 农村寡妇一级毛片免费看视频| 手机永久免费的AV在线电影网| 一级特级aaaa毛片免费观看| a级片免费在线观看| 久久久久成人精品免费播放动漫| 91精品免费高清在线| 丁香花免费高清视频完整版| 成人免费无码大片a毛片软件| 国产成人无码免费视频97 | 最近中文字幕免费2019| **俄罗斯毛片免费| 好男人视频社区精品免费| 四虎影永久在线高清免费| 国产精品亚洲综合一区| 久久久亚洲精品国产| 亚洲人成在线播放| 国产成人高清亚洲一区91| 中文字幕版免费电影网站| 91短视频在线免费观看| 欧美男同gv免费网站观看| 亚洲Av无码乱码在线znlu| 亚洲国产精品无码久久久蜜芽 | 亚洲国产精品VA在线观看麻豆| 亚洲五月激情综合图片区| 亚洲精品天堂在线观看| 四虎国产精品永免费| 久久九九AV免费精品|