java中的默认访问说明符和受保护的访问说明符之间的区别 [英] Difference between the default access specifier and protected access specifier in java

查看:158
本文介绍了java中的默认访问说明符和受保护的访问说明符之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习java,当我通过访问说明符时,我有一个疑问,如果没有指定默认值和受保护的访问说明符之间有什么区别?

I was trying to learn java and when I went through access specifiers I had a doubt, what is the difference between the default one if none is specified and the protected access specifier?

推荐答案

protected 说明符允许所讨论的类的所有子类访问它们所包含的任何包,以及到同一个包中的其他代码。默认说明符允许同一包中的其他代码进行访问,但不允许通过驻留在不同包中的子类中的代码进行访问。请参阅 Java语言规范第6.6节

The protected specifier allows access by all subclasses of the class in question, whatever package they reside in, as well as to other code in the same package. The default specifier allows access by other code in the same package, but not by code that is in subclasses residing in different packages. See Java Language Specification Section 6.6.

编辑:根据MichaelSchmeißer的要求(所以其他人不必阅读评论或按照链接查找):所有接口成员都是隐含地公开。实际上,为 public 以外的接口成员指定任何访问说明符都是编译时错误(尽管没有访问说明符默认为公共访问)。以下是来自JLS的类成员规则的完整规则(请参阅以上关于包,顶级类和接口以及数组的规则的链接):

Per request of Michael Schmeißer (so others don't have to read through the comments or follow a link to find this): all members of interfaces are implicitly public. It is, in fact, a compile-time error to specify any access specifier for an interface member other than public (although no access specifier at all defaults to public access). Here's the full set of rules from the JLS for class members (see the above link for the rules for packages, top-level classes and interfaces, and arrays):


只有在类型可访问且成员或构造函数时,才能访问引用(类,接口或数组)类型的成员(类,接口,字段或方法)或类类型的构造函数。声明允许访问:

A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:


  • 如果成员或构造函数被声明为public,则允许访问。

  • If the member or constructor is declared public, then access is permitted.

接口的所有成员都是隐式公开的。

All members of interfaces are implicitly public.

否则,如果成员或构造函数被声明为protected,只有在满足下列条件之一时才允许访问:

Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:



  • 从包含声明受保护成员或构造函数的类的包中发生对成员或构造函数的访问。

  • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

访问是正确的,如§6.6.2。 (此子句引用允许派生类访问超类的受保护成员的规则;§6.6.2starts:对象的受保护成员或构造函数可以从仅在其中声明它的包外部访问负责实现该对象的代码。然后详细说明。)

Access is correct as described in §6.6.2. (This clause refers to the rules that allow derived classes to access protected members of superclasses; §6.6.2 starts: "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." It then elaborates on that.)




  • 否则,如果成员或构造函数被声明为私有,那么当且仅当它出现在顶级类的主体内时才允许访问(§7.6)附上声明成员或构造函数。

    • Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

      否则,我们说有默认访问权限,只有在类型为的包中发生访问时才允许访问声明。

      Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.

      这篇关于java中的默认访问说明符和受保护的访问说明符之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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