java - package

 

  • Concept of packages

Current case: all classes are written directly in the src

Problems:

  1. There are many classes in the src, harder to find
  2. Team development in different functional modules exist if the class with the same name can not co-exist in the same directory

(Ie can not exist in a file with the same name in the same directory)

 

  1. Role package

(1) resolve naming conflicts, specify the class name in a class at the same time, specify a package name

grammar:

package package name;

note:

  1. package statement must be placed in the class declaration of the first sentence
  2. package statement in a class can only have one (because of the nature of the package is actually a file directory, so for a document, it can only belong to one directory, so can not appear multiple package statement)

 

(2) may be similar or functionally similar classes in a package, and to facilitate maintenance organizations

(3) the packet may have a hierarchical structure, the scale can be divided finer classes, the functions, modules, etc. may be organized in different categories package

(4) defines the level of access to the package

https://www.cnblogs.com/DreamDrive/p/4641676.html (Form is very important !!!)

 

  1. Naming convention package

review:

All names do not use the Pinyin

  1. Variable: See name known meaning, hump nomenclature
  2. Method: see the name EENOW hump nomenclature, verb or use, not more than four parameters
  3. Class Name: See-known name meaning, first letter capitalized and the rest follow the hump nomenclature
  4. static final constants:

/ **
 * constants categories:
 * constants: fixed value, the value can not be modified
 * /
public class AppConstant {
    / * static constant named, all in all uppercase letters ** /
    / **
     * If a plurality of constant names of words, using _ spaced between each word, i.e., the serpentine nomenclature
     ** /
    public static String ACCESS_TOKEN Final = "12345678";
    public static Final String [] = STRs new new String [2];

    public static void main ( String [] args) {
        // reflect the characteristics of a static constant static, i.e., can be accessed directly by the class name
        System.out.println (AppConstant.ACCESS_TOKEN);
        // also reflects the characteristics of a static constant final, i.e., the line can not be modified
        / = /AppConstant.token "123";
        ; System.out.println (AppConstant.ACCESS_TOKEN)
        element array of values corresponding to the index in the heap is 0 // strs [0] = "Hello " is modified
        // strs = new String [3] ; equivalent to the original strs address has been changed, and strs are final modified, it is not possible to change the
        strs [0] = "the Hello";
// strs = new new String [3 ];

    }

    public void Method () {
        / **
         * for some small changes value, if written directly in the code, is not conducive to maintaining referred to as "magic number", referred to as "magic number"
         * these values may be direct defined as constants, the calling process if the value is changed, only need to modify the straight line constant
         * /
        System.out.println ( "token:" + ACCESS_TOKEN);
        System.out.println ( "token:" + ACCESS_TOKEN );
    }

    public void the method1 () {
        System.out.println ( "token:" + ACCESS_TOKEN);
        System.out.println ( "token:" + ACCESS_TOKEN);
    }
}

 

Package naming convention:

(1) The package can have a hierarchy, the name of a little by dividing the packet to be a hierarchical structure of the package

(2) All in all lowercase letters package, can not appear keywords and reserved words

(3) Naming packages typically include: domain name anti-write the project name + + + module name component name + ......

 

  1. Introducing the package
  1. Why do you want to import

In one class, because, additionally to use a class provided in the package, then you must import the package

  1. grammar:

import package name + class name;

note:

Before a.import statement appears after the package statement, class declaration

b. java.lang class all the package is imported automatically, without manual introduction

c. it can also be defined by a global name (package name + class name) to import classes import statement can be omitted, but too much trouble.

java.util.Scanner sc = new java.util.Scanner(System.in);

//java.util.Date
a Date = new new date1 a Date ();
//java.sql.Date
// To use the same name for different packet classes, you should specify a global name defined
java.sql.Date date2 = new java. sql.Date (123);

d. * Do by introducing the next packet all classes, e.g.

import java.util. *;

What class to use it to import what class

Guess you like

Origin blog.csdn.net/weixin_43727372/article/details/90080154