zoukankan      html  css  js  c++  java
  • protected访问权限

    Java中protected方法访问权限的问题

    protected 修饰的成员变量或方法,只能在同包或子类可访问;

    package 1

    public class TestPackage
    {
    protected String name;

    protected void f1()
    {
    TestPackage tt = new TestPackage();
    System.out.println("ccc");
    }

    }

    package 2;

    public class TestPackage2 extends TestPackage
    {

    protected void f2()
    {
    TestPackage tt = new TestPackage();

    tt.f1();// tt 的f1()方法与TestPackage2 不同包,所以不能访问,若同包则可以访问。

    TestPackage2  t2 = new TestPackage2 ();

    t2.f1();//t2 继承了TestPackage, 所以t2有f1方法,并且属于在同一包,可以访问

    System.out.println("ccc");
    }

    }

     -----------------------------------------------------------------------

    先看Test.java

     

    此时出现上文提到的错误: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的子类。但是,不能在一个子类(Test)中访问另一个子类(MyObject)的protected方法,尽管这两个子类继承自同一个父类(Object)。其实这是由于调用MyTest的Clone方法实际上是调用MyTest的父类Object的Clone方法。第一,Object和Test不在一个包内;第二,虽然Test是Object的子类,但是不是Test自己调用Object中的protected方法Clone而是调用MyTest的父类的Clone方法。所以没有protected的访问权限。

     

    再看示例2:

    Test2.java

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

    编译通过的原因显而易见,当你在MyObject2类中覆盖clone()方法时,MyObject2类和Test2类在同一个包下,所以此protected方法对Test2类可见。

    分析到这里,我们在回忆一下 Java中的浅复制与深复制,章节2.2中的声明,②在派生类中覆盖基类的clone()方法,并声明为public。现在明白这句话的原因了吧(为了让其它类能调用这个类的clone()方法,重载之后要把clone()方法的属性设置为public)。

    下面再来看示例3:

    Test3.java

    这里我用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.

    翻译:protected访问是需要一些准备的。假如类A定义了一个protected的属性x,并且被定义在不同包中的类B扩展了类A。A和B不再同一个包内这一点非常重要。从而,B继承了A的protected属性x,而且在当前B 的实例中这个属性是能够被访问的又或者其他代码中涉及到访问这个属性的B的实例中也是可以访问这个属性的。然而,这并不表示B 的代码可以任意访问A的实例中protected修饰的属性!如果一个对象是A而不是B的实例,显然B是没有继承该对象的属性的,从而B的代码无法访问它们。

     

    方法的访问控制:

     

    public

    protected

    default

    private

    同类

    T

    T

    T

    T

    同包

    T

    T

    T

     

    子类(不同包)

    T

    T

     

     

    不同包中无继承关系的类

    T

     

     

     

    本文对网上一篇文章修改的,增加了自己的理解和注释。原文地址:http://zhangjunhd.blog.51cto.com/113473/19287/

  • 相关阅读:
    UVA 1025 A Spy in the Metro DP水题
    ZOJ 3814 Sawtooth Puzzle BFS
    ZOJ 3816 Generalized Palindromic Number
    UVA 10859 Placing Lampposts 树形DP
    UVA 11825 Hackers' Crackdown 状压DP
    POJ 2887 Big String 线段树 离线处理
    POJ 1635 Subway tree systems Hash法判断有根树是否同构
    BZOJ 3110 k大数查询 & 树套树
    sdoi 2009 & 状态压缩
    来自于2016.2.24的flag
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9314320.html
Copyright © 2011-2022 走看看