Six access Java programming ideas of control

Access Control (or hide specific implementation) and "The initial implementation is not appropriate" relevant.
Access control levels, from the largest to the smallest privileges privileges were: public, protected, package access (no keywords) and private.
Will build bundled into a cohesive library unit: Java is controlled by the keyword package, and the amount of access to the presence of a qualifier is the same package, or a separate package affected.

6.1 package: the library unit

The package includes a set of classes that are organized together under a single name space.
Importing a single class, the class may be named in the import statement.

Full control of the namespace in java and create a unique identifier combinations can resolve name conflicts for each class.
When writing a java source code file, which is commonly referred to as a compilation unit. Each compilation unit must have a suffix .java. In the coding unit may have a public class that must be the same file name.

6.1.1 Code Organization

When compiling a .java files, each class in the .java files output file will have the same output file name of each class, more than just a .class suffix.
Java can run the program is a set of packaged and compressed into a Java .class file document file. Java interpretation is responsible for finding these files, loading, explained.
Library is actually a set of class files, wherein each file has a public class, and any number of non-public class. Thus each file a member. If you want the members belonging to the same group, you can use the keyword package.
If you use the package statement, it must file a comment in addition to the first sentence of the program code. In the beginning of the file write:

package access;

package and import keywords allow you to do, it is the single global name space separated.

6.1.2 Creating unique package names

The process of running Java interpreter: First, find the environment variable CLASSPATH, CLASSPATH contains one or more directories used to find the root of the .class file. Start from the root directory, the package name acquisition and interpretation replaced backslash each period to produce a path name from the CLASSPATH root. The resulting path will be connected to the various items in the CLASSPATH, interpret it to find information about the name of the class you want to create .class files in those directories.
Compiled code is typically placed in a different directory from the source, but must ensure JVN use CLASSPATH can find the path.

6.2 Java access modifiers

6.2.1 package access

Default access has no keyword, usually the package access.
Packet access allows all relevant classes encapsulated combined, so that they can easily interact with each other.
The only way to get access to a member are:

  • 1. The change to become a member of the public.
  • 2. Access through the qualifiers without access to the other classes are placed in the same package gives members a way to access the package.
  • 3. Only two classes are in the same package, it can access other members of the pack access.
  • 4. The device provides access to methods and mutation to read and change the value.
6.2.2 public: Interface Access

After using the keyword followed by members of the public, it means own public statement is available to everyone.

6.2.3 private: You can not access

In addition to the class that contains the member, any other class can access this member.

6.2.4 protected: inheritance access

Used to treat inherited access.

6.3 interface and implementation

Access control is often referred to as hidden concrete implementation. Method and packaged into data class, and hide implementation, often referred to collectively packaged . The result is a data type with characteristics and behaviors simultaneously.
For two important reasons, access to the border control authority will draw on internal data type.
The first reason: the setting can not be used and limit use.
The second reason: the separation of the interface and realization.

Access 6.4 class

Access qualifier page can be used to determine which class library for users of the library are available.
There are some additional limitations:

  • 1. Each compilation unit can have only one public class.
  • 2.public class name must exactly match the name of the file containing the compilation unit.
  • Without the complete compilation unit 3. public class is also possible, in this case, the file name can be freely.

For access to the class, only two choices: package access or oubic.
If you do not specify an access modifier for the class access, it will default to package access.

Guess you like

Origin www.cnblogs.com/Tan-sir/p/11228600.html