简体   繁体   English

“包私有”成员访问是否与默认(无修饰符)访问同义?

[英]Isn't "package private" member access synonymous with the default (no-modifier) access?

I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access."我对某些文档使用的术语“私有包”以及“默认访问”的用法感到有些困惑。 Aren't package-private and default access both synonymous with protected?包私有和默认访问不是受保护的同义词吗?

Yes, it's almost the same.是的,几乎一样。 The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition , by a subclass of its class in another package. protected 修饰符指定该成员只能在其自己的包中访问(与包私有一样),此外,还可以由另一个包中其类的子类访问。

The "default" access modifier (the one where none of them are explicitly given) is "package-private", which means only things in the same package can access them. “默认”访问修饰符(没有明确给出的访问修饰符)是“package-private”,这意味着只有同一个包中的东西才能访问它们。 However, being in the same package implies nothing about the inheritance relationship between classes -- it's purely a naming convention.然而,在同一个包中并不意味着类之间的继承关系——这纯粹是一种命名约定。

"Protected" means that not only classes in the same package, but also subclasses (regardless of which package those subclasses are in) will be able to access it. “受保护”意味着不仅同一个包中的类,而且子类(无论这些子类在哪个包中)都可以访问它。

The default access for classes is package-private, however the default access for interface members is public.的默认访问权限是包私有的,但是接口成员的默认访问权限是公共的。

eg例如

public interface I {
   int A = 1;
// same as
   public static final int A = 1;

   void method();
// same as
   public abstract void method();

   class C { }
// same as
   public static class C { }
}

The default access rules for interfaces are not the same as for classes.接口的默认访问规则与类不同。

Package-private and default access are synonyms.包私有和默认访问是同义词。 An object can also access protected member of the objects whose classes are in the same package.对象还可以访问其类在同一包中的对象的受保护成员。 An object can also access protected member of its superclasses without a condition about their package.对象也可以访问其超类的受保护成员,而无需对其包进行任何条件。 As a concrete example :作为一个具体的例子:

package ab;

class A {
   protected void foo() {}
   void dd(){}
}

class C {
   void aa(){
       A a = new A();
       a.foo(); //legal
       a.dd();  //legal
   }
}


package sub;

class D extends A{
      void ac(){
         foo(); //legal ..
         dd();  //illegal.. because dd has default access.. 
      }

class E {
    void ee(){
       A a = new A();
       a.foo(); //illegal
       a.dd();  //illegal     
    }

'Package private' and default access are the same. “包私有”和默认访问是相同的。 In early releases of the compiler around 1.1.2/3, 'package' was an allowed modifier, but ignored, meaning the same as no modifier, ie 'package private'.在 1.1.2/3 左右的编译器早期版本中,'package' 是一个允许的修饰符,但被忽略,这意味着与没有修饰符相同,即 'package private'。 Shortly afterwards there was a short lived fashion for putting /*package*/ (as a comment) in such situations.不久之后,在这种情况下放置/*package*/ (作为评论)有一种短暂的时尚。 Similarly at that time you could declare things like synchronized classes, although again there was no actual semantic effect.同样,在那个时候你可以声明像同步类这样的东西,尽管同样没有实际的语义效果。

Neither of them is the same as 'protected', which extends to derived classes in other packages.它们都与“受保护”不同,后者扩展到其他包中的派生类。

From Java Language Spec来自Java 语言规范

  • 6.6.5 Example: Default-Access Fields, Methods, and Constructors If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package. 6.6.5 示例:默认访问字段、方法和构造函数如果没有指定 public、protected 或 private 访问修饰符,则类成员或构造函数可在包含类的声明的整个包中访问声明了成员,但在任何其他包中都无法访问类成员或构造函数。

If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package如果公共类具有具有默认访问权限的方法或构造函数,则此方法或构造函数不能被在此包外声明的子类访问或继承

default and package-private both are same, which means both can be used by any class till they are in same package. defaultpackage-private都是相同的,这意味着它们可以被任何类使用,直到它们在同一个包中。

The package-private term, actually, is termed by the meaning of private modifier as private means it is available only in same class and no other classes or subclasses can access it within same package or without.包私有术语实际上是由private修饰符的含义命名的,因为私有意味着它仅在同一个类中可用,并且在同一个包中或不在同一个包中,其他类或子类都不能访问它。

Hence package-private means same as default .因此package-private 的意思与default相同。

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

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