Home  >  Article  >  Java  >  A summary of packages in Java

A summary of packages in Java

little bottle
little bottleforward
2019-04-08 16:24:533209browse

A package in Java can be understood as a folder , because it is a path with a tree structure. When we use package When declaring a package under a certain package, we must also put its .class in the directory named after the package, otherwise the class will not be found.

To put a class into a package, you need to use the package statement. In fact, when we do not use the package statement, our classes are placed in a default default package. This This is also why when we run a Java program, there cannot be a .class file with the same name in the running directory.

In addition, A complete class name is the package name class name. In the absence of import, using a class requires giving the complete class name, such as java.util.Date. For convenience, Java automatically imports the two packages java.lang and the default package of the current file, that is to say: (1) Java stipulates that other packages do not need to give the complete class name when using classes in the java.lang package; (2) ) Classes in the default package can be used with each other (or under the same package) without giving the complete class name.

There are the following rules for mutual access between packaged and unpackaged classes: (1) Unpackaged classes can access packaged classes, which can be imported directly Yes; (2) Classes with packages cannot access classes without packages. Direct access is from the same package. Since different packages need to be imported, classes without packages cannot be imported; (3) When defining a class, in order to So that others can use it, so you must define a package.

Java packages appear to be hierarchical (from the path point of view), but in fact they are not. For example, java.awt and java.awt.color are two packages with the same status. java.awt.color does not belong to java.awt.

When using a class in a class name, first look for this class in the current package, and find it in the package that is not introduced.

Members of the package

Some associated classes, interfaces, enumerations, annotation types, resource files used by classes in the package such as pictures, etc.

The role of the package

(1) Gather related interfaces and classes together

For example, package graphics; put graphics processing related modules together

(2) Avoid Eliminates naming conflicts between types: as a kind of encapsulation, packages provide multi-layered namespaces for classes.

(3) Realizes external access control to packages

Use of packages (declaration and import of packages)

In general, the use of packages can be summarized as Three steps: how to bundle classes and interfaces into a package, how to use the classes in the package, and how to arrange the file system so that the compiler can find the source files.

1. Define the package

1. Declaration with package class

Use the package keyword in the first line of the source code to define the package. The package name is usually all lowercase, with "." separated.

Bag's naming specifications See Link: https://segmentfault.com/a/1190000008711718

## https://docs.oracle.com/javase/tutorial/package/namingpkgs. The declaration of html

package has the following principles: (1) The package statement must be placed at the beginning of the source file; (2) A source file can only have one package statement; (3) The package name must be all lowercase to avoid confusion with the class , interface name conflict.

2. Compile the class with package

javac -d directory source file name.java

3. Run the class with package

java package name .Class name

2. Using packages

Access between classes in the same package is relatively simple and can be used directly with the class name in the code; while access between different packages must meet a Condition: If a class wants to be accessed by classes in other packages, it must be modified with public (note: the class modified with public must have the same name as the file name). There are usually three methods for use between different packages: (1) Use the full name to access the class; (2) Import the class; (3) Import the entire package containing the class

1. Use the full name

Suppose there is a graphics package with a Circle class (public modification), then outsourcing access to it is as follows:

graphics.Circle cir = new graphics.Circle();

2. Import the class

Above The method of using the full name is a bit cumbersome. If you want to abbreviate or use this class frequently, you can do this:

import graphics.Circle;

public class PackageTest {
    public static void main(String[] args) {
        Circle cir = new Circle();
    }
}

3. Import the entire package

When we want to use more classes of this package, For example, for the Line class, you can import all classes in the package:

import graphics.*;

public class PackageTest {
    public static void main(String[] args) {
        Circle cir = new Circle();
        Line line = new Line();
    }
}

三、管理文件系统

许多Java平台的实现依赖于文件系统的等级制度 来管理源文件和class文件。我们一般把类、接口、枚举、注释类型放在可以反映包名的目录中,比如,Rectangle.java属于graphics类,我们就放在.....\graphics\Rectangle.java的路径上。同样,我们对其进行编译产生的class文件也要放在类似的目录上(这个目录最好不要和源文件放在一起),这样的好处是当我们要复用某个类时,就知道到哪去找了,JVM在运行程序时也知道去哪里找类。

比如,源文件放在:86a15073ee07a5288ef78984c38cc44e\sources\com\example\graphics\Rectangle.java

class文件放在:1c88c48f018631bbf99e30d7795bbcba\classes\com\example\graphics\Rectangle.class,其中,1c88c48f018631bbf99e30d7795bbcba\classes是类路径(class path),由CLASSPATH系统变量进行设置,而包名是com.example.graphics。编译器、JVM通过将包名称添加到类路径来构造.class文件的路径。

所以编译器、JVM在路径1c88c48f018631bbf99e30d7795bbcba\classes\com\example\graphics中查找类文件。class path可以有好几条,由分号(Windows)或冒号(UNIX)分隔。 默认情况下,编译器、JVM搜索当前目录和包含Java平台类的JAR文件,所以这些目录自动位于类路径中。

CLASSPATH是JVM或Java编译器中的一个参数,它指定用户定义的类和包的位置。 可以在命令行上或通过环境变量设置。默认情况下,我们访问JDK标准API和扩展包时,无需设置查找位置。 而用户自定义的包和库,则必须设置。由于JVM会在当前目录下搜索,所以当我们的文件都在运行目录下时,也可以不设置CLASSPATH。

有关CLASSPATH可以参考:

https://en.wikipedia.org/wiki/Classpath_%28Java%29

https://en.wikipedia.org/wiki/Java_Classloader

关于Import

关于import,有三个注意的地方。

1、

首先是,Java的包表面上的等级问题。从路径上看,包似乎是分层的,但它们不是。 例如,Java API包括java.awt包,java.awt.color包,java.awt.font包等等。但是,java.awt.color包、java.awt.font包和其他java.awt.xxxx包并不属于java.awt。 前缀java.awt(The Java abstract window toolkit)的作用只是告诉开发者——和窗口有关的包都在这里面。但是包和包之间并不是包含关系。

当你导入java.awt.*时,会导入java.awt包中的所有类型,但它不会导入java.awt.color,java.awt.font或任何其他java.awt.xxxx包的所有类型。 如果你要使用java.awt.color中的类以及java.awt中的类,则必须同时导入导入这两个包:

import java.awt.*;
import java.awt.color.*;

2、

其次是名字含糊不清的问题。当你导入的两个包中有一个类名字相同时,如果你继续采用缩写,就会出现一些问题。这时,你就要使用这个类的全名。比如,java.util、java.sql包中都有Date类,程序又同时导入了这两个包中的Date类。

import java.sql.*;
import java.date.*;

3、static import

当我们需要频繁访问某个类的常量和静态方法,反复写它们的前缀(全名)可能不利于代码阅读。 静态import语句就提供了一种导入要使用的常量和静态方法的方法,这样您就不需要为其名称添加前缀。比如java.lang.Math类定义PI常量和许多静态方法,包括计算正弦,余弦,切线,平方根,最大值,最小值,指数等。

import static java.lang.Math.PI;
import static java.lang.Math.cos;

public class ImportTest {
    public static void main(String[] args) {
        double r = Math.cos(Math.PI * 2);
		double r1 = Math.cos(PI * 2);
		double r2 = cos(PI * 2);
		System.out.println(r+" "+r1+" "+r2);    
    }
}

当然,你也可以直接全部导入:

import static java.lang.Math.*;

应该注意的是,使用静态导入时要非常谨慎。 过度使用静态导入会导致代码难以阅读和维护,因为阅读者不知道某个特定的静态对象是哪个类的。 不过,当我们恰当地使用静态导入时,代码可以因为不用繁琐地写全名而更具可读性。

 【推荐课程:Java视频教程】   

The above is the detailed content of A summary of packages in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete