Second, Java object-oriented (7) _ package idea - package statement

2018-04-30

 

package statement

 

First, the package keyword

  To better organize classes, Java provides a package mechanism for distinguishing namespaces of class names.

  The role of the package: 

    1. Organize classes or interfaces with similar or related functions in the same package to facilitate the search and use of classes.

    2. Like folders, packages are also stored in a tree-like directory. The names of classes in the same package are different, and the names of classes in different packages can be the same. When calling classes with the same class name in two different packages at the same time, the package name should be added to distinguish them. Therefore, packages can avoid name collisions.

    3. Packages also limit access rights. Only classes with package access rights can access classes in a package.

  Java uses the package (package) mechanism to prevent naming conflicts, access control, provide search and location of classes (classes), interfaces, enumerations (enumerations) and annotations (annotation) and so on.

 

  Package statement syntax format: package package name. subpackage name. sub-sub-package name

  This statement must be placed as the first line of code in the java file (before all code.)

 

Second, the practical usage of package

  1. How the package name is defined

  

Java package names are composed of lowercase words, and the first letter of the class name is capitalized; the path of the package conforms to the definition of the developed system module, such as production to production, material to material, and basic class to basic class. In order to see the package name to understand which module is, so as to directly find the corresponding implementation in the corresponding package.

 

 

 

    Due to the object-oriented characteristics of Java, each Java developer can write his own Java Package. In order to ensure the uniqueness of the naming of each Java Package, in the latest Java programming specification, developers are required to add a prefix to the package name defined by themselves. unique prefix. Because domain names on the Internet are not repeated, most developers use their company's domain name on the Internet as the only prefix for their packages. For example: com.sun.swt. ….

 

 

 

    Therefore, we know that the general company name is "com.company name.project name.module name....".
    So, how do we name our personal projects?

 

    After my search for "personal" words, there are "individual, personal, private, one-man", further analysis of the meaning of the above 4 words, and to ensure the uniqueness, use the first 4 letters of each word As a prefix, it happens to be distinguished from "com". as follows:

 

    indi :

 

         Individual projects refer to projects initiated by individuals but not completed by themselves. They can be public or private projects, and the copyright mainly belongs to the initiator.

 

         The package name is " indi.InitiatorName.ProjectName.ModuleName ....".

 

    pers :

 

         Personal projects refer to projects initiated by individuals, completed by themselves, and can be shared. The copyright mainly belongs to individuals.

 

         The package name is " pers .personname.projectname.modulename . …".

 

    priv :

 

         Private projects refer to projects initiated by individuals, completed by themselves, non-public and privately used, and the copyright belongs to individuals.

 

         The package name is " priv.personname.projectname.modulename . …".

 

    onem  :

 

         Same as "indi", "indi" is recommended.

 

 

 

    In addition, in order to distinguish the difference between the team project and the project mentioned above, I have an extension:

 

    team :

 

         Team project refers to the project initiated by the team and developed by the team, and the copyright belongs to the team.

 

         The package name is " team.teamname.projectname.modulename . …".

 

    with  :

 

         For company projects, the copyright is owned by the company that initiated the project.

 

         The package name is " com.companyname.projectname.modulename . …".

 

   

    

  

  2. Package name in java (jdk)

  

 

  Focus on these:

  

 

 

 

    

 

    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325052764&siteId=291194637