java package

When it comes to a package, it is a container in which many things can be stored, and a package in java is used to hold classes, so that you can easily organize your own code and manage your own code separately from the code base provided by others. , to avoid confusion.


The reason why java is so powerful is that java has a powerful class library, and the class library is organized using packages.

Standard java packages have a hierarchy. Just like hard disk directory nesting, packages can also be organized using nesting levels.

All standard java packages are in the java and javax package hierarchy.


Packages can well determine the uniqueness of class names, and Sun recommends using the company's Internet domain name as the package name in reverse order. Obviously, this further determines the uniqueness of class names. For example, suppose my registered domain name is beibeidezhuye.com, then I can create a package named com.beibeidezhuye to manage my code.


Forehead. . . Having said so much, it seems that I haven't mentioned how to use the package. Here's how to use the package and put your own classes into the package:

The use of packages is nothing more than using classes from other packages and putting your own classes in one package

The operation of using classes in other packages is called importing a package (class) , and according to the import method, it can be divided into import and static import .

Putting your own classes in a package can write the package package name at the top of your own code .


So, here's one by one:

First look at the import method. There are two import methods: the first method is to add the complete package name before each class name, and the second method is to use the import keyword to import.

example:

The first:


The second:


Note: .* is called a wildcard.

Or directly specific to a class:



Then there is static import. The function of static import is to use the imported static method and static domain without adding a class name prefix. Of course, it can also be specific to a certain method.

Example 1:


Example 2 (importing specific methods)



Here's how to put your own classes into a package :

That's right, just import the package name (you can go to the specific class), it's simple and domineering.

Example:


This class is now in the package com.zhangyanjie

If at this point you are thinking of using console commands to compile this file, you can use a command similar to the following:

javac com/zhangyanjie/PackageTest.java

Then when running, you have to use the following command:

java com.zhangyanjie.PackageTest


In fact, the package is not a mysterious thing. If you use the file manager to go to your project directory to check, you will find that you have created a new folder and put your classes in it, so why use the command It's no surprise that you need to add the path when you compile it, clam.


Having said so much, we can import classes and put our own classes into packages. However, there are several issues worthy of our attention:

(1) When using the * sign, only one package can be imported, and there cannot be an import statement similar to import java.* or import java.*.* .

(2) It is not ruled out that the same class name, variable name or constant name is used in the two imported packages. At this time, you should write a special sentence to import the class, variable name or constant name in which package. If both packages need to be used, then there is no way but to use the first import method mentioned above.

(3) Although we feel that import is the same as #include of c++, the two have nothing in common. The latter (c++) must use #include to load the declaration of external features, because the latter (c++) ) compiler cannot look inside any file except the one being compiled and explicitly included in the header file, whereas the former (java) compiler can look inside other files, just tell it where to look. In C++, similar to the package mechanism are namespaces.

(4) The scope of the package is default (actually there is no such keyword).

Guess you like

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