默认(包)访问级别的类中的默认(包)和公共访问级别是否有任何区别?

Rom*_*man 5 java oop

代码中的相同问题:

class Foo {

   int getIntProperty () { ... }

   CustomObject getObjectProperty () { ... }

   void setIntProperty (int i) { ... }

   void setObjectProperty (CustomObject obj) { ... }

   //any other methods with default access level    
}
Run Code Online (Sandbox Code Playgroud)

VS

class Foo {

   public int getIntProperty () { ... }

   public CustomObject getObjectProperty () { ... }

   public void setIntProperty (int i) { ... }

   public void setObjectProperty (CustomObject obj) { ... }

   //any other methods with public access level   
}
Run Code Online (Sandbox Code Playgroud)

idr*_*sid 5

当你子类Foo时有区别:

public class Bar extends Foo {

}
Run Code Online (Sandbox Code Playgroud)

然后尝试另一个包:

new Bar().getIntProperty ()
Run Code Online (Sandbox Code Playgroud)

它将在你的第二个例子中编译(所有方法都是公共的)但不是第一个(所有方法默认访问)