Java:跨包的受保护访问 [英] Java: protected access across packages

查看:121
本文介绍了Java:跨包的受保护访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解下面示例中正在发生的事情(通过子类从包外部访问受保护的成员)。

I would like to understand what's happening in the example below (where a protected member is being accessed from outside the package through a subclass).

我知道类在包外部,子类只能通过继承来查看受保护的成员。

I know for classes outside the package, the subclass can see the protected member only through inheritance.

有两个包: package1 package2


  1. package1 ProtectedClass.java

package org.test.package1;

public class ProtectedClass {

    protected void foo () {
        System.out.println("foo");
    }
}


  • package2 ExtendsprotectedClass.java

    package org.test.package2;
    
    import org.test.package1.ProtectedClass;
    
    public class ExtendsprotectedClass  extends ProtectedClass {
    
        public void boo() {
            foo(); // This works, 
                   // since protected method is visible through inheritance
        }
    
        public static void main(String[] args) {
            ExtendsprotectedClass epc = new ExtendsprotectedClass();
            epc.foo(); // Why is this working? 
                       // Since it is accessed through a reference,
                       // foo() should not be visible, right?
        }
    }
    


  • package2 UsesExtendedClass.java

    package org.test.package2;
    
    public class UsesExtendedClass {
    
        public static void main(String[] args) {
            ExtendsprotectedClass epc = new ExtendsprotectedClass();
            epc.foo(); // CompilationError: 
                       // The method foo() from the type ProtectedClass
                       // is not visible
        }
    }
    


  • 据了解, boo() ExtendsprotectedClass 中的方法可以访问 foo(),因为受保护的成员只能通过继承访问。

    It is understood that the boo() method in ExtendsprotectedClass can access foo(), since protected members can be accessed through inheritance only.

    我的问题是,为什么 foo()方法在通过 main中的引用访问时工作正常() ExtendsprotectedClass 的方法,但通过 epc 将无效 c $ c>引用 UsesExtendedClass

    My question is, why is the foo() method working fine when accessed through a reference in the main() method of ExtendsprotectedClass but will not work when accessed through the epc reference in UsesExtendedClass?

    推荐答案

    ExtendsprotectedClass 类允许通过类型 ExtendsprotectedClass ProtectedClass 的受保护成员C $ C>。来自 JLS第6.6.2节

    Code within the ExtendsprotectedClass class is allowed to access protected members of ProtectedClass via a reference of type ExtendsprotectedClass. From the JLS section 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.


    令C为声明受保护成员m的类。只允许在C的子类S的主体内访问。此外,如果Id表示实例字段或实例方法,则:

    Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:


    • 如果访问是通过限定名称Q.Id,其中Q是ExpressionName,则当且仅当表达式Q的类型为S或S的子类时才允许访问。[...]

    UsesExtendedClass 不适用于实施 ExtendsprotectedClass ,因此最终的调用失败。

    UsesExtendedClass isn't reponsible for the implementation of ExtendsprotectedClass, hence the final call fails.

    编辑:这背后的原因是 protected access旨在帮助子类实现他们需要的功能,从而提供对超类内部的访问权限,而不是通常可用的内部。如果所有代码都可用,那么将该方法公之于众。基本上,子类被信任不破坏封装;他们在自己类型的对象中获得了更多的能力。公共API不应公开这些细节,但受保护的API只能用于为子类提供更多机会。

    The reasoning behind this is that protected access is designed to help subclasses implement the functionality they need, giving more access to the internals of the superclass than would normally be available. If that were available to all code, it would be pretty close to making the method public. Basically, the subclasses are trusted not to break encapsulation; they're given more capabilities within objects of their own type. The public API shouldn't expose those details, but the protected API can just for the purposes of giving subclasses more opportunities.

    这篇关于Java:跨包的受保护访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆