protected子类可见为什么在子类中调用有时会编译出错?

package family;
        
public class Student {
        
    protected void tt(){
        System.out.println("I am a student...");
    }    
}

///调用类
package temp;

public class Doctor extends family.Student{
    
    void ss(){
        new family.Student().tt(); // compile-time error    
    }
}

 我觉得这个问题其实挺典型的,问题出在“protected子类可见”这种介绍比较笼统吧

 

 


The fields x and y are declared protected and are accessible outside the package points only in subclasses of class Point, and only when they are fields of objects that are being implemented by the code that is accessing them.

在包外访问proteced成员只有在其声明类的子孙类中且只有当它们是访问它们的代码所实现的对象的成员时

比如
package temp;

public class Doctor extends family.Student{
   
    void ss(){
        new family.Student().tt();
    }
}

这里虽然是在子类中,但非访问它们的代码所实现的对象,应该是Doctor,而非Student

 

package temp;

public class Doctor extends family.Student{
   
    void ss(){
        new family.Doctor().tt();
    }
}

至于为什么是这样?我想初步的可能就是为了防止使用Student的客户直接访问吧,是直接访问,
尽可能地防止误用而产生的错误,就需要用到这些

 

Consider this example, where the points package declares:

 

package points;
public class Point {
        protected int x, y;
        void warp(threePoint.Point3d a) {
                if (a.z > 0)         // compile-time error: cannot access a.z
                        a.delta(this);
        }
}

and the threePoint package declares:

 

package threePoint;
import points.Point;
public class Point3d extends Point {
        protected int z;
        public void delta(Point p) {
                p.x += this.x;          // compile-time error: cannot access p.x
                p.y += this.y;          // compile-time error: cannot access p.y

        }
        public void delta3d(Point3d q) {
                q.x += this.x;
                q.y += this.y;
                q.z += this.z;
        }
}

which defines a class Point3d . A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p , because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p ). The method delta3d can access the protected members of its parameter q , because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d .

The method delta could try to cast (§5.5 , §15.16 ) its parameter to be a Point3d , but this cast would fail, causing an exception, if the class of p at run time were not Point3d .

A compile-time error also occurs in the method warp: it cannot access the protected member z of its parameter a , because while the class Point (the class in which the reference to field z occurs) is involved in the implementation of a Point3d (the type of the parameter a ), it is not a subclass of Point3d (the class in which z is declared).

 

附参考资料:

http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2

http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值