2018-04-30
Package Statement
First, the Package keyword
To better organize classes, Java provides a package mechanism for distinguishing the namespace of a class name.
The role of the package:
1. Organize similar or related classes or interfaces in the same package to facilitate the search and use of classes.
2. Like folders, packages are stored in a tree-like directory. The class names in the same package are different, and the names of the classes in different packages can be the same, and the package name should be distinguished when calling two classes of the same class name in different packages. Therefore, packages can avoid name collisions.
3. The package also restricts access, and classes that have access to the package can access the classes in a package.
Java uses the package as a mechanism to prevent naming conflicts, access control, to provide search and locate classes (class), interfaces, enumerations (enumerations), and annotations (annotation), and so on.
Package Statement Syntax format:package name. Package name. Child Child Package Name
The statement must be used as the first line of code in the Java file (before all code.) )
Second, the package practice usage
- How package names are defined
Java package names are composed of lowercase words, the class name is capitalized, and the path of the package conforms to the definition of the developed system module, such as production to production, material to material, base class to basic class. In order to see the package name to understand which module, so directly to the corresponding package to find the corresponding implementation.
Due to the Java object-oriented nature, each Java developer can write its own Java package, in order to guarantee the uniqueness of each Java packages naming, in the latest Java programming specification, developers are required to add a unique prefix to their own definition. Because domain names on the Internet are not duplicated, most developers use their company's domain name on the Internet as the only prefix for their packages. For example: COM.SUN.SWT .....
Thus, we know that the general company is named "com. Company name." Project name. Module name ....
So, how do we name our individual projects?
After my "personal" word search, there are "individual, personal, private, one-man", further to the above 4 words meaning analysis, and in the guarantee of uniqueness, using the first 4 letters of each word as a prefix, exactly and " Com "also made a distinction. As follows:
Indi :
An individual project, a project that is initiated by an individual but not done on its own, may be public or private, and the copyright is primarily the originator.
The package is named "Indi. Initiator name." Project name. Module name ... ".
Pers :
Individual projects, which are individual initiation, completion alone, shareable items, are mainly personal.
The package is named "Pers. Personal name." Project name. Module name ... ".
priv :
A private project, which is an individual-initiated, self-fulfilling, privately-used project, which belongs to the individual.
The package is named "priv. Personal name." Project name. Module name ... ".
onem :
As with "Indi", it is recommended to use "Indi".
In addition, in order to differentiate between the team project and the previously mentioned project, I also have a bit of an extension:
Team :
A team project, a project initiated by a team and developed by the team, is owned by the team.
The package is named"team." The name of the project name. Module name ....
com :
Company projects, copyright by the project initiated by the company owned.
The package is named "com. Company name." Project name. Module name ... ".
Package name in 2.java (JDK)
Focus on these types of:
Second, Java object-oriented (7) _ Packaging Ideas--package statement