博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你真的不懂protected
阅读量:7155 次
发布时间:2019-06-29

本文共 2479 字,大约阅读时间需要 8 分钟。

  hot3.png

来谈谈protected访问权限问题。看下面示例1:

Test.java

class MyObject {}public class Test {    public static void main(String[] args) {       MyObject obj = new MyObject();       obj.clone(); // Compile error.    }}

此时出现上文提到的错误:The method clone from the type Object is not visiuable.

我们已经清楚Object.clone()是protected方法。这说明,该方法可以被同包(java.lang)下和它(java.lang.Object)的子类访问。这里是MyObject类(默认继承java.lang.Object)。

同样Test也是java.lang.Object的子类。但是,不能在一个子类中访问另一个子类的protected方法,尽管这两个子类继承自同一个父类。

再看示例2:

Test2.java

class MyObject2 {    protected Object clone() throws CloneNotSupportedException {       return super.clone();    }}public class Test2 {    public static void main(String[] args) throws CloneNotSupportedException {       MyObject2 obj = new MyObject2();       obj.clone(); // Compile OK.    }}

这里,我们在MyObject2类中覆盖(override)父类的clone()方法,在另一个类Test2中调用clone()方法,编译通过。

编译通过的原因显而易见,当你在MyObject2类中覆盖clone()方法时,MyObject2类和Test2类在同一个包下,所以此protected方法对Test2类可见。(为了让其它类能调用这个类的clone()方法,重载之后要把clone()方法的属性设置为public)。

下面再来看示例3:

Test3.java

package 1class MyObject3 {    protected Object clone() throws CloneNotSupportedException {       return super.clone();    }}package 2public class Test3 extends MyObject3 {    public static void main(String args[]) {       MyObject3 obj = new MyObject3();       obj.clone(); // Compile error.       Test3 tobj = new Test3();       tobj.clone();// Complie OK.    }}

这里我用Test3类继承MyObject3,注意这两个类是不同包的,否则就是示例2的情形。在Test3类中调用Test3类的实例tobj的clone()方法,编译通过。而同样调用MyObject3类的实例obj的clone()方法,编译错误!

意想不到的结果,protected方法不是可以被继承类访问吗?

必须明确,类Test3确实是继承了类MyObject3(包括它的clone方法),所以在类Test3中可以调用自己的clone方法。但类MyObject3的protected方法对其不同包子类Test3来说,是不可见的。

这里再给出《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.

顺便说两句,国内的很多Java书籍在介绍访问权限时,一般都这样描述(形式各异,内容一致):

方法的访问控制:

 

public

protected

default

private

同类

T

T

T

T

同包

T

T

T

 

子类(不同包)

T

T

 

 

不同包中无继承关系的类

T

 

 

 

转载于:https://my.oschina.net/fuyong/blog/739151

你可能感兴趣的文章
Ali流量控制中间件Sentinel
查看>>
微信小程序里多出来的奇怪宽度
查看>>
Babel 命令基础
查看>>
Java中的static关键字解析
查看>>
2个rman自动恢复的脚本
查看>>
香港药品 ref
查看>>
spring学习总结一----控制反转与依赖注入
查看>>
健康日志7-11
查看>>
模式匹配之尺度空间---scale space
查看>>
makefile编写---单个子目录编译自动变量模板ok
查看>>
MBR (主引导记录)
查看>>
win10汇编环境的搭建
查看>>
周末学习记录(摘抄为主)
查看>>
智能ERP 交接班统计异常的解决方法
查看>>
UnityWebSocket
查看>>
我的目录
查看>>
MatlabSourceCode
查看>>
MatlabTrick
查看>>
读库存扣减系列文章有感
查看>>
文本对齐方式
查看>>