Java uses three explicit keywords to set the boundaries in a class: public, private, and protected. Their use and meaning are quite straightforward. These access specifiers determine who can use the definitions that follow. public means the following element is available to everyone. The private keyword, on the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type. private is a brick wall between you and the client programmer. Someone who tries to access a private member will get a compile-time error. The protected keyword acts like private, with the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly.
Java also has a “default” access, which comes into play if you don’t use one of the aforementioned specifiers. This is usually called package access because classes can access the members of other classes in the same package, but outside of the package those same members appear to be private.
本文详细介绍了Java中public、private及protected三个关键字的作用与用法,并解释了默认访问权限的概念,帮助读者理解如何通过这些访问修饰符来控制类成员的可见性。
1128

被折叠的 条评论
为什么被折叠?



