ArticleDirectory
Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
We have written some javaProgram. Each previous Java program is saved as a file, such as test. java. Then, the program is compiled as test. Class. We use$ Java TestTo run the program.
However, in a normal Java project, we often need to write more than one. Java program. The final Java product includes all Java programs. Therefore, Java needs to solveOrganize Java programs.Package)The purpose is to better organize Java programs.
Package Creation
Package creation is very simple. We only use the Java programPackageYou can. Let's take the human class as an example and put it into the package:
Package Com. vamei. Society; Public Class Human { /** * Constructor */ Public Human ( Int H ){ This . Height = H; system. Out. println ( "I'm born" );} /** * Accessor */ Public Int Getheight (){ Return This . Height ;} /** * Mutator */ Public Void Growheight ( Int H ){ This . Height = This . Height + H ;} Private Int Height ;}
The first line of the preceding statement
Package com. vamei. Society;
Indicates that the programCom. vamei. SocietyPackage. Com. vamei (vamei.com) indicates the package author'sDomain Name(Unfortunately, this domain name has been registered by someone else, so it only serves as a demo ). Java requires that the package have a domain name prefix to distinguish different authors. Society is the further local path name. Com. vamei. Society compose the package name.
The package providesNamespace). The complete path of a Java class consists of its package and class name, such as com. vamei. Society. Human. The corresponding human. Java program should be placed under COM/vamei/Society. Classes are completely identified by paths, so different packages can have classes with the same name, and Java will not be confused. For example, Com. vamei. Society. Human and COM. vamei. creature. Human are two different classes.
Let's look at the details. The human class is public and its constructor is public, so any other object can call this class. As we have said before, a Java program can only have one public class, which must go to the. Java file with the same name. A class can have no public keyword, and it actually represents a permission: the class in itsVisible in package. That is, other Java programs in the package can access this class. This is in JavaDefault access permission.
Likewise, the object member can be the default permission (visible in the package ). For example, we remove the public keyword before the getheight () method.
Package call
We only need to put the human. Class compiled by human. Java into the corresponding folder. For example, I put human. class into COM/vamei/Society. In fact, you can also put the. Java file into the corresponding path, and Java will automatically compile during use.
If the entire package (that is, the com folder)Located in the current working path, You can use the package without special settings, such as testagain. Java below:
ImportCom. vamei. Society .*;Public ClassTestagain {Public Static VoidMain (string [] ARGs) {HUMAN aperson=NewHuman (180); System. Out. println (aperson. getheight ());}}
ImportUsed to identify paths. ExploitationImportStatement, we can introduce the class in the corresponding path.*All classes in the society folder are introduced. In testagain, we directly use the human class.
We can also provideComplete path. This can distinguish classes with the same name but different paths, such:
Public ClassTestagain {Public Static VoidMain (string [] ARGs) {com. vamei. Society. Human aperson=NewCom. vamei. Society. Human (180); System. Out. println (aperson. getheight ());}}
Because we provide a complete class path, you do not need to use the Import Statement.
If the packageNot put in current working pathWhen using the package, we need to notify Java. For example, we put the package in/home/vamei/javapackage, so that the human. the class is located in/home/vamei/javapackage/COM/vamei/Society/human. class, and our working path is/home/vamei. In this way, the package cannot be found. One method is to use javac and Java-ClasspathThe path of the folder where the package is located, for example:
$ Javac-classpath/home/vamei/javapackage:. testagain. Java
$ Java-classpath/home/vamei/javapackage:. testagain
Find the package from/home/vamei/javapackage and working path. Java can find the human class from/home/vamei/javapackage, and testagain class from.
You can also setClasspathEnvironment variable, add the above path to the variable, instead of entering the-classpath option every time.
Similar to the package mechanism is also common in other languages, such as the import mechanism in Python. They are all for better organization and use of existing programs. Using the package, we can easily expand the Java program and use the existing Java library. Note that the package management is. Class file. Java"One compilation, run everywhere"(Compile once, run anywhere).. Class files can be stored in anyInstalledJava Virtual Machine(JVM, Java Virtual Machine) platform, which helps us overcome the difficulty of program porting caused by system differences.
The differences between systems can be very large. If we use C language to write programs, we need to re-compile the source program on each platform to adapt to different hardware conditions. The Java Virtual Machine connects the platform and the Java universe. It forms the middle layer of hardware and programming logic. JVM hides hardware differences and provides programmers with a "standard" Java universe. The. Class file can be seen as the currency circulating in the Java universe. With the help of package management in the infrastructure of JVM, Java programs have achieved good performance.Portability(Portability ).
Summary
Package, import
Default permission: visible in the package
-Classpath and classpath
Welcome to continue reading the "Java quick tutorial" series of articles