理解protected

          Posted on 2006-07-06 18:13 負人博客 閱讀(321) 評論(0)  編輯  收藏 所屬分類: JAVA技術

          對于protected關鍵字經常在使用中弄混,特寫一小例進行測試
          Protected方法或屬性例子:


          Package com.first;

          Public class Test1 {
          ?Protected String a;
          Protected void test() {
          ? System.out.println(“it is a test”);
          }
          }
          package com.second;
          import com.first.*;
          public class Test2 extends Test1 {
          ?Test1 test1 = new Test1();
          Test2 test2 = new Test2();
          test1.a = “test”;//錯誤
          test2.a = “test”; //正確
          test1.test();//錯誤
          test2.test();//正確
          }
          ///原因,protected定義的東西在不同包內不能訪問,只有繼承的子類可以訪問


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


          網站導航:
           

          posts - 26, comments - 5, trackbacks - 0, articles - 8

          Copyright © 負人博客

          主站蜘蛛池模板: 天祝| 长沙县| 玛沁县| 安仁县| 文登市| 松滋市| 巴彦淖尔市| 韶山市| 临城县| 宾阳县| 沽源县| 随州市| 普定县| 丽江市| 江华| 塘沽区| 临猗县| 丰镇市| 克什克腾旗| 江都市| 嘉善县| 皮山县| 惠水县| 隆安县| 葫芦岛市| 镶黄旗| 彭山县| 滨海县| 双流县| 玉龙| 孝感市| 鄯善县| 漳浦县| 佛坪县| 德惠市| 牙克石市| 孝感市| 新邵县| 固始县| 剑阁县| 清镇市|