谈谈 java 的protected 访问权限。

今天做项目时遇到一个问题。
以为对同一包内的一个类调用另一个类的protected 方法感到很奇怪。
然以为protected 需要派生类才能访问,没想到在同一包内也能访问protected 成员。
哎!弄了 好久才明白。
我的测试代码:
基类代码:包为com.fu

package com.fu;

public class Base {
	
		protected int protectedInt;  //protected成员
		public int publicInt;        //public 成员
		protected  int  getProtectedInt(){
			return protectedInt;
		}
		protected static int staticProtectInt;
		private int privateInt;
		public static void main(String[] args) {
			Base b=new Base();
			b.privateInt=5;
		}
}

不同包内派生类:com.zi
package com.zi;

import com.fu.Base;

public class TestOtherPackageSubClass extends Base {
	public void test() {
		Base b = new Base(); // 使用基类
		b.publicInt = 0; // 可以访问
		//b.protectedInt=3; // 访问protected的属性失败
		//b.getProtectedInt(); // 访问protected的方法失败
		Base.staticProtectInt=9;
	}
	
	public void test2() {
		//privateInt =6;   //访问父类的私有-出错
		publicInt = 0; // 可以访问
		protectedInt = 2; // 直接用父类的属性-成功
		super.protectedInt=4;
		
		getProtectedInt(); // 直接用父类的方法-成功
		System.out.println("getProtectedInt-test2:"+getProtectedInt());
		
		protectedInt = 22; // 直接用父类的属性-成功
		Base.staticProtectInt=10;
		super.getProtectedInt(); // 直接用父类的方法-成功
		System.out.println("getProtectedInt-test2:"+getProtectedInt());
	}
	
	public void test3(){
		TestOtherPackageSubClass t=new TestOtherPackageSubClass();
		t.protectedInt=6;
		t.getProtectedInt();
		System.out.println("getProtectedInt-test3:"+t.getProtectedInt());
	}
	
	public static void main(String[] args) {
		TestOtherPackageSubClass t=new TestOtherPackageSubClass();
		t.test2();
		System.out.println(Base.staticProtectInt);
	}
}

运行结果为:
getProtectedInt-test2:4
getProtectedInt-test2:22
10
从运行结果 4,22 看出  test2 中不管有没有写super.protectInt,还是直接protectInt 都是指向父类的成员。从这可以看出不在同一包的子类可以访问父类数据成员。
而标颜色的是明显出错的。
如果把这个Base.staticProtectInt=10; 去掉的话, Base.staticProtectInt输出结果为0。因为test()方法没有调用不会设为9。

同一包内子类代买 com.fu
package com.fu;

import com.zi.TestOtherPackageSubClass;

public class TestSamePackageSubClass extends Base {
	public void test() {
		Base b = new Base(); // 使用基类
		b.publicInt = 0; // 可以访问
		b.protectedInt=3; // 访问protected的属性成果
		b.getProtectedInt(); // 访问protected的方法成功
	}
	
	public void test2() {
		publicInt = 0; // 可以访问
		protectedInt = 2; // 直接用父类的属性-成功
		Base.staticProtectInt=9;
		System.out.println("getProtectedInt-test2:"+getProtectedInt());
	}
	
	public void test3(){
		TestOtherPackageSubClass t=new TestOtherPackageSubClass();
		t.protectedInt=6;
		
		System.out.println("getProtectedInt-test3:"+t.getProtectedInt());
	}
	
	public static void main(String[] args) {
		TestSamePackageSubClass t=new TestSamePackageSubClass();
		t.test2();
		t.test3();
		
	}

}

如果同一包内的话可以通过 base b.protectedInt 访问了。
同一包内非派生类:
package com.fu;

public class TestSamePackageNotSubClass {
	public static void main(String[] args) {
		Base b = new Base(); // 使用基类
		b.publicInt = 0; // 可以访问
		b.protectedInt=3; 
		System.out.println(b.getProtectedInt());
	}
}
同一包内的话可以通过 base b.protectedInt 访问了。
从这看出同一包可以使用基类 Base b.protectedInt 访问,而不属于同一包内的派生类中只能直接使用基类super.protectedInt 或者protedtecInt,而不能通过 Base b.protectedInt 访问。
 
3种权限总结如下:
privte   :只用本类可用 
protected   :同包内均和派生类中可用  比默认[default|package](只包内能使用) 权限大一点
public   :全部可用
在看一个代码明白protected 和package 的权限
package com.zi;

import com.fu.Base;

public class TestOtherPackageNotSubClass {
	public static void main(String[] args) {
		System.out.println(Base.staticProtectInt); //访问出错。
	}
}
TestOtherPackageNotSubClass 不能访问Base.staticProtectInt,而TestOtherPackageSubClass能够访问正说明了这一点。






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值