java访问控制 protected值得注意的一点

昨天在孙卫琴的《java面向对象编程》7.1节---访问控制修饰符
关于一个protected的例子 发现了一个容易被忽视的错误

一般来说 访问控制分4种级别:
公开:public 同类 同包 子类 不同包 都可以访问
默认:只向同包同类放开
私有:private 只有类本身可以访问
保护:protected 向子类以及同一个包中的类放开

来看一下在该节中的例子
先定义一个ClassA 并把它放在mypack1包中
package mypack1;
public class ClassA {
public int var1;
protected int var2;
int var3;
private int var4;

public void method(){
var1=1;
var2=1;
var3=1;
var4=1;

ClassA a = new ClassA();
a.var1=1;
a.var2=1;
a.var3=1;
a.var4=1;
}
}

然后又在另外一个包 mypackage2中 存在ClassA的一个子类 ClassC
package mypack2;
import mypack1.ClassA;
class ClassC extends mypack1.ClassA{
public void method(){
ClassA a = new ClassA();
a.var1=1;
a.var2=1; //此行出错
}
}

实际上这个例子有问题
你会看到ide(或者编译时)在 a.var2=1 这一行报错 提示不能访问protected对象
这就是protected经常被人忽视的地方
尽管ClassC是ClassA的一个子类
但是在ClassC中创建的是ClassA的一个实例
该实例中的protected成员变量则很明显没有被ClassC继承到
自然在ClassC中无法访问var2

所以对于这种情况 将代码改为如下,则可以编译通过。

package mypack2;
import mypack1.ClassA;
class ClassC extends mypack1.ClassA{

public void method(){
ClassA a = new ClassA();
a.var1=1;
super.var2=1;
ClassC c = new ClassC();
c.var1=1;
c.var2=1;
}
}

OK,用java in a nutshell中的一段话来总结一下全文:
protected access requires a little more elaboration. Suppose class A declares a protected field x and is extended by a class B, which is defined in a different package (this last point is important). Class B inherits the protected field x, and its code can access that field in the current instance of B or in any other instances of B that the code can refer to. This does not mean, however, that the code of class B can start reading the protected fields of arbitrary instances of A! If an object is an instance of A but is not an instance of B, its fields are obviously not inherited by B, and the code of class B cannot read them.
 from: http://norno.blogbus.com/logs/2006/12/4015425.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值