in Java, a package is used to solve a class name conflict problem, which is a set of related classes and interfaces
Advantages of the package: avoid a large number of class name conflicts and enlarge the namespace.
Package embodies the encapsulation mechanism.
Packagestr;Interfacetemplate{LongFactorial (intm);LongMypower (intMintn);}classToolImplementstemplate{ Public LongFactorial (intm) { intSum=1; for(inti=1;i<=m;i++) Sum*=i; returnsum; } Public LongMypower (intMintN) { intsu=m; for(inti=2;i<=n;i++) Su*=m; returnsu; }} Public classfive3{ Public Static voidMain (string[] args) {tool T=Newtool (); System.out.println ("The factorial of 5 is:" +t.factorial (5)); Tool L=Newtool (); System.out.println ("4 of the 1-time side is:" +t.mypower (4,1)); }}
in this code example, the first line of the required Java file is written package name;
Package Name: The package you want to create gives it a proper name.
After adding this statement to save, you can execute this command under DOS window to create the package named Str:
Javac-d. Five3.java
Note that javac behind the space ,-D after the space, the space behind the dot must be taken, not less!!! , this statement needs to be executed in the Java file directory.
When you're done, you'll find that a folder is generated in your file,
because the first line in the Java code is written to the package str, the str folder is generated, and he is the one that created it.
Because of the three classes created in the Java code, this place will be placed under the created package, so how to execute him, very simple, only need to enter a command to
Java str.five3
Note: The format for this is:Java package name. Class Name//This class name needs to contain the main method to be executed, generally called the main class.
The results of the implementation are as follows:
Commands for creating packages under the Java DOS window