理解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 © 負人博客

          主站蜘蛛池模板: 临泽县| 徐闻县| 芷江| 曲沃县| 临潭县| 淅川县| 拜城县| 扶余县| 嘉禾县| 昭觉县| 西贡区| 满洲里市| 确山县| 墨竹工卡县| 泸西县| 太保市| 陵水| 雅江县| 武城县| 明溪县| 互助| 松潘县| 伊春市| 合作市| 望城县| 都安| 黄山市| 白水县| 邳州市| 深州市| 浦县| 布尔津县| 焦作市| 巫溪县| 桑日县| 同德县| 霍林郭勒市| 遂川县| 盐池县| 玛沁县| 安宁市|