java 的protected 访问权限。

基类代码:包为com.fu

  1. package com.fu;  
  2.   
  3. public class Base {  
  4.       
  5.         protected int protectedInt;  //protected成员  
  6.         public int publicInt;        //public 成员  
  7.         protected  int  getProtectedInt(){  
  8.             return protectedInt;  
  9.         }  
  10.         protected static int staticProtectInt;  
  11.         private int privateInt;  
  12.         public static void main(String[] args) {  
  13.             Base b=new Base();  
  14.             b.privateInt=5;  
  15.         }  
  16. }  

不同包内派生类:com.zi
  1. package com.zi;  
  2.   
  3. import com.fu.Base;  
  4.   
  5. public class TestOtherPackageSubClass extends Base {  
  6.     public void test() {  
  7.         Base b = new Base(); // 使用基类  
  8.         b.publicInt = 0// 可以访问  
  9.         <span style="color:#FF0000;">//b.protectedInt=3; // 访问protected的属性失败  
  10.         //b.getProtectedInt(); // 访问protected的方法失败</span>  
  11.         Base.staticProtectInt=9;  
  12.     }  
  13.       
  14.     public void test2() {  
  15.         <span style="color:#FF0000;">//privateInt =6;   //访问父类的私有-出错</span>  
  16.         publicInt = 0// 可以访问  
  17.         protectedInt = 2// 直接用父类的属性-成功  
  18.         super.protectedInt=4;  
  19.           
  20.         getProtectedInt(); // 直接用父类的方法-成功  
  21.         System.out.println("getProtectedInt-test2:"+getProtectedInt());  
  22.           
  23.         protectedInt = 22// 直接用父类的属性-成功  
  24.         Base.staticProtectInt=10;  
  25.         super.getProtectedInt(); // 直接用父类的方法-成功  
  26.         System.out.println("getProtectedInt-test2:"+getProtectedInt());  
  27.     }  
  28.       
  29.     public void test3(){  
  30.         TestOtherPackageSubClass t=new TestOtherPackageSubClass();  
  31.         t.protectedInt=6;  
  32.         t.getProtectedInt();  
  33.         System.out.println("getProtectedInt-test3:"+t.getProtectedInt());  
  34.     }  
  35.       
  36.     public static void main(String[] args) {  
  37.         TestOtherPackageSubClass t=new TestOtherPackageSubClass();  
  38.         t.test2();  
  39.         System.out.println(Base.staticProtectInt);  
  40.     }  
  41. }  

运行结果为:
getProtectedInt-test2:4
getProtectedInt-test2:22
10
从运行结果 看出  test2 中不管有没有写super.protectInt,还是直接protectInt 都是指向父类的成员。从这可以看出不在同一包的子类可以直接(未实例化)访问父类数据成员

如果把这个Base.staticProtectInt=10; 去掉的话, Base.staticProtectInt输出结果为0。因为test()方法没有调用不会设为9。

同一包内子类代买 com.fu
  1. package com.fu;  
  2.   
  3. import com.zi.TestOtherPackageSubClass;  
  4.   
  5. public class TestSamePackageSubClass extends Base {  
  6.     public void test() {  
  7.         Base b = new Base(); // 使用基类  
  8.         b.publicInt = 0// 可以访问  
  9.         b.protectedInt=3// 访问protected的属性-成功 
  10.         b.getProtectedInt(); // 访问protected的方法-成功  
  11.     }  
  12.       
  13.     public void test2() {  
  14.         publicInt = 0// 可以访问  
  15.         protectedInt = 2; // 直接用父类的属性-成功  
  16.         Base.staticProtectInt=9;  
  17.         System.out.println("getProtectedInt-test2:"+getProtectedInt());  
  18.     }  
  19.       
  20.     public void test3(){  
  21.         TestOtherPackageSubClass t=new TestOtherPackageSubClass();  
  22.         t.protectedInt=6;  
  23.           
  24.         System.out.println("getProtectedInt-test3:"+t.getProtectedInt());  
  25.     }  
  26.       
  27.     public static void main(String[] args) {  
  28.         TestSamePackageSubClass t=new TestSamePackageSubClass();  
  29.         t.test2();  
  30.         t.test3();  
  31.           
  32.     }  
  33.   
  34. }  

如果同一包内的话可以通过  b.protectedInt 访问了。
同一包内非派生类
  1. package com.fu;  
  2.   
  3. public class TestSamePackageNotSubClass {  
  4.     public static void main(String[] args) {  
  5.         Base b = new Base(); // 使用基类  
  6.         b.publicInt = 0// 可以访问 -成功
  7.         b.protectedInt=3;   
  8.         System.out.println(b.getProtectedInt());  
  9.     }  
  10. }  
同一包内的话可以通过 base b.protectedInt 访问了。

同一包子类可以直接或实例化(new)访问protedted成员,普通类可以实例化(new)访问protedted成员不可直接访问,而不属于同一包内的派生类中只能直接使用基类super. protedted成员 或者protedted成员,而不能通过 实例化访问(new) 访问。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章