22- Definition and Use of Package

Definition and use of packages

In the actual development, there must always be the concept of a package. The package can be used to achieve the packaging of classes. In the actual development in the future, all the classes must be placed in the package.

Definition of package

For the project, it is not developed by one person. Often there will be 2-3 people in a team to realize the project business. At this time, we must face the problem: the definition of the same name of the class.
In the operating system, it is clearly defined that the same program file is not allowed to be stored in the same directory, but it is difficult to avoid duplication in development. At this time, the program file can be placed in a different directory, and this directory is called a package, directory==package

package cn.mldn.demo; //定义包,其中.表示分割子目录(子包)

Once there is a package in the program development, the "x.class" file must be saved in the specified directory after the program is compiled at this time, but the manual resume is very troublesome, and the package compilation process can be carried out at this time.
java -d . Hello.java

  • -d: indicates the generation directory
  • ".": Means to generate program files in the current directory

When the program is executed, the program class must be executed with the package java cn.mldn.demo.Hello. From then on, the complete class name is "package. class".

Package import

Using the package definition, you can actually save classes with different functions in different packages, but these classes have a mutual calling relationship. At this time, you need to use the import statement to import; you
need to pay attention to the difference between public class and class definition ?

  • Public class: The class name must be consistent with the file name. Only one public class is allowed in a *.java file. If it is to be used by other packages, then this class must be defined as public;
  • class: The class name can be inconsistent with the file name. Multiple class definitions can be provided in a *.java file, and different *.class files are formed after compilation, but these classes can only be accessed by this package, and cannot be accessed by outsourcing;
  • In actual development, only one class program class is often provided in a *.java source code file, and this program class is generally defined by public class;
  • The package name in the program class must be defined in lowercase letters, for example, cn.mldn.utils

For new problems, there are times when many classes in a certain package are used. It is troublesome to import them separately, so you can use "*" to handle them.
import cn.mldn.demo.*;
Even if you use the package.*, it does not mean that all are loaded. The required program classes will be loaded according to your own needs, and the unnecessary program classes will not be loaded.
But using package.* to import, then there will be a little trouble: it is possible that two different packages have the same class name, which may lead to "unclear reference to the class". At this time, you should write the full name ( new cn.mldn.utils.Message()) to operate.

Static import

Assuming that all methods in a class are static methods, then the "package. class" where the program is located should be imported according to the original method. These static methods are then called through the class name.
Starting from JDK1.5, it is possible to use static import processing for special classes that are all provided by static methods in the class;
import static cn.mldn.utils.Mymath.*;
when static import processing is used, it is as if the method is directly defined in the main class and can be directly called by the main method .

Generate jar file (jar command)

When the project is completed, there will be a large number of *.class files, and the management of these files can often be processed in a compressed structure. Such results are called jar files in Java and are completed by using the jar command;

  1. Define a program class
  2. Compile and package the program:
    • Package and compile the program: javac -d. Message.java
    • At this time, the cn package is formed, and there are corresponding sub-packages and *.class files in the package, and package it as mldn.jar: jar -cvf mldn.jar cn
      • -c: Create a new jar file;
      • -v: get detailed output;
      • -f: Set the name of the generated jar file;
  3. Each *.jar file is an independent program path. If you want to use this path in a Java program, you must configure it through CLASSPATH;

If the program is compiled and passed, but the class cannot be loaded due to a change in CLASSPATH, the following error message will appear when the program is executed: "Exception in thread "main" java.lang.NoClassDefFoundErroe: certain class", there is only one type of error In case, the *.jar package is not configured correctly.

System common packages

Since the development of Java, there are a large number of supporting libraries, which are generally composed of two aspects:

  • Provided by Java itself (in addition to the class library provided by the JDK, there will be some standards)
  • Java support class libraries provided by third-party vendors to complete various required functions;

And a large number of class libraries will be provided in the JDK, encapsulated in unused development kits

  • java.lang: String, Number, etc.
  • java.lang.reflect: reflection mechanism processing package, all design starts from here;
  • java.util: implement the definition of tool classes, including the definition of data structure;
  • java.io: input and output stream operation package;
  • java.net: package for network program development;
  • java.sql: database programming development kit;
  • java.applet: Java's most primitive form of use, a program type that is directly nested on the web page;
  • java.awt, javax.swing: Java graphical interface development kit (GUI), where awt is a heavyweight component, and swing is a lightweight component;

Access control authority

There are three main characteristics in the object-oriented development process: encapsulation, inheritance, and polymorphism. Then for encapsulation, the main realization depends on the access control permissions: private, default, protected, public

No Access range private default protected public
1 Same category in the same package can can can can
2 Different classes in the same package can can can
3 Subcategories of different packages can can
4 All classes in different packages can

In the entire access control authority processing, only protected authority is a relatively new concept. The following is an explanation:
There are three corresponding access permissions in the package in the program: private, default, and protected, but if you use it each time, you can differentiate and compare it. Trouble, reference:

  • As long as the definition of attributes, all use private
  • As long as the definition of the method, all use public

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/111400731