为什么我看到那么多的例子在字段前键入private,而默认情况下afaik字段是私有的.
private int number;
int number;
//Both of these are the same afaik. Yet, in a ton of examples private gets fully written, why?
Run Code Online (Sandbox Code Playgroud)
Aug*_*ust 18
不,他们不一样.缺少访问修饰符默认为package-private.
请参阅:http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Run Code Online (Sandbox Code Playgroud)
此规则的例外是interface方法和字段签名是public在省略访问修饰符时:http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html