有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么受保护的实例成员在不同包的子类中不可见,而受保护的类成员却可见?

package one;

public class A {
    protected int first;
    protected static int second;
}

package two;

import one.A;

public class B extends A {
    public void someMethod() {
        this.first = 5; //works as expected
        B.second = 6; //works
        A a = new A();
        // a.first = 7; does not compile

        //works just fine, but why?
        a.second = 8; 
        A.second = 9;
    }
}

为什么不将相同的限制应用于静态字段,其背后的想法是什么


共 (1) 个答案

  1. # 1 楼答案

    JLS 6.6.2

    A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

    6.6.2.1

    Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

    this.first = 5;之所以有效,是因为BA的实现者

    A.second之所以有效,是因为此限制仅为对象的成员定义。这同样适用于B.second

    至于为什么是这样指定的,你必须询问定义规范的人——我们只能做出假设6.6.2.1甚至有一个例子表达了类似的问题

    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;
        }
    }
    

    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.


    我建议你去Why we should not use ^{} in Java看看

    protected的语义是针对实例成员的-protected staticprotected的目的相矛盾,这可能是它没有以同样的方式受到限制的原因