简体   繁体   English

受保护的访问修饰符

[英]Protected access modifier

JLS 6.6.2 gives us the following restriction for the package-access of protected members. 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. 对象的受保护成员或构造函数只能从负责该对象实现的代码在其中声明该对象的包外部访问。

What did they mean responsible for implementation. 他们是什么意思负责执行。 Couldn't you get an example? 你不能举个例子吗?

It means that you can't access a protected super-class member of a different instance of the same class. 这意味着您无法访问同一类的不同实例的受保护超类成员。

package one;
public class A {protected int b;}

package two;
public class B extends A {

    public void someMethod (A other)
    {
        b = 5; // allowed
        other.b = 5; // not allowed
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM