protected访问权限

    技术2026-08-13  1

    来谈谈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类可见。 分析到这里,我们在回忆一下Java中的浅复制与深复制文中,章节2.2中的声明,在派生类中覆盖基类的clone()方法,并声明为public现在明白这句话的原因了吧(为了让其它类能调用这个类的clone()方法,重载之后要把clone()方法的属性设置为public)。 下面再来看示例3 Test3.java package 1 class MyObject3 { protected Object clone() throws CloneNotSupportedException {        return super.clone();     } }   package 2 public 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类的实例tobjclone()方法,编译通过。而同样调用MyObject3类的实例objclone()方法,编译错误! 意想不到的结果,protected方法不是可以被继承类访问吗? 必须明确,类Test3确实是继承了类MyObject3(包括它的clone方法),所以在类Test3中可以调用自己的clone方法。但类MyObject3protected方法对其不同包子类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       所以我想说的是,多读些英语原版书籍。

    本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/19287

    最新回复(0)