代码改变世界

TIJ——Chapter Six:Access Control

2015-06-11 09:10  星星之火✨🔥  阅读(230)  评论(0编辑  收藏  举报

package:the library unit 

The levels of access control from "most access" to "least access" are publicprotected, package access(which has no keyword), and private.

Each compilation unit must hava a name ending in .java, and inside the complication unit there can be a public class that must hava the same name as the file(including capitalization, but excluding the .java file name extension). There can be only one public class in each compilation unit; otherwise, the compiler will complain. If there are additional classes in that compilation unit, they are hidden from the world outside that package because theyre not public, and they comprise "support" classes for the main public class.

 

Java access specifiers

|_protected: inheritance access

Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general. Thats what protected does. protected also gives package access-that is, other classes in the same package may access protected elements.

 

Class access

倒包的一些限制:

1、There can be only one public class per compilation unit(file). The idea is that each compilation unit has a single public interface represented by that public class. It can hava as many supporting package- access as you want. If you hava more than one public class inside a compilation unit, the compiler will give you an error message.

2、The name of the public class must exactly match the name of the file containing the compilation unit, including capitalization. So for Widget, the name of the file must be Widget.java, not widget.java or WIDGET.java. Again, youll get a compile-time error if they dont agree.

3、It is possible, thought not typical, to hava a compilation unit with no public class at all. In this case, you can name the file whatever you like(although naming it arbitrarily will be confusing to people reading and maintaining the code).