In java learning, the use of package packages, the import key words (small records in java learning), and packageimport
Learning java,PackagePackage usage and import of the import key words (small record in java learning)Author: Star)
Use of packages
Package: You can put the compiled class file into the specified package.
Java package: equivalent to a folder on a computer.
Definition package: package keyword definition package
Create a package file during compilation:
ConsoleCmdInput: javac-d. Class Name. java // create a folder specified by the package. Created in the current path.
(Accessed)
(Accessing the Dod class)
Note:
1. classes must be public-modified and public.
2. The accessed class must be compiled first.
3. To use other classes in other classes, use the full name of the class. (This method is too troublesome, so we have a better method to export packages)
Keyword of the import package: import
The preceding code can be optimized to import dog. Dog to the header file.
Then, the following Dog class can be directly created without writing the package name: Dog dog = new Dog ();
Export package format:
1. import package name 1. Package name 2. Package name 3... class name;
2. import package name. *; import all directly. We recommend that you use the first package import method.
Benefits of using packages:
1. It can prevent conflicts of class files with the same name.
2. Separate the source file from the compiled file.
Precautions for using the package:
1. Only one package can be defined in the Java class
(This is wrong)
2. The package must be the first statement to describe the class.
Knowledge Point exercises:
You can define two classes in two different packages, and then call each other for the two classes ~