7.1 Overview of the basic package
Due:
We designed a program in time (especially cooperative multiplayer), write some classes to implement functions, but there is often a phenomenon of the same name, in order to solve this problem, it is specially designed package. (There are other effects, below)
Simple to understand: the same name exist between cells in different cities, the city name with the same name can be distinguished from those cells, it will be understood that the city name as the above packages, the cell can be seen as the same name of the class, by prefix, solve the problem of the same name.
(1) concept:
In order to better organize the class, Java provides a package mechanism for the difference between the class name of the namespace.
In fact, it can be understood as that folder, and use the storage directory tree.
(2) the role of:
A: the same name as the class distinction
B: manage your classes
a: division by function - similar or related functions of the class or interface of the tissue in the same package
b: Module according to points
(3) the definition of the packet:
package package name;
// multi-level package with separately.
(4) Note:
A: package statement must be the first statement in the file is valid
B: In a java file, only one package
(5) Package with the compile and run
Mastery - automatic (Dos in)
javac - . d . xxx the Java
// can be used on behalf of all current * .java source files in the directory // running: the Java package name. the HelloWorld
In the structure ecplise, ideal like IDE package will automatically generated name
7.2 guide package
When you need to use a member of a package, we need to import the package in java program
If the two classes in the same package, the package need not be turned
format:
The first:
// Import single (recommended)
Import package name;
Note: We will come with a guide who. (As little as possible with an asterisk * below)
The second:
// import all the classes in java.io (not recommended)
Import the Java. IO. *
note:
// before the last sentence instead of two if you can use
Import the Java. Lang. *;
Import . The Java IO. *; Import the Java. * ; // no substitute for the above two, can only point to a single-package
Note: Use the contents of the package java.lang is no guide, you can view specific jdk-api
effect:
Use of a package corresponding members, and to simplify the writing
For example, we want to use BufferedReader in java.io package
We can
import java.io.BufferedReader
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
If no import package guide
When we use java library, you need to write the full path name of the class, the code clearly too long
java.io.BufferedReader in = new java.io.BufferedReader(new FileReader("foo.in"));
7.3 Permissions modifier
After reading the basic concept of packages, we also understand the original spoken permissions modifier issue
This class | Under the same package | Subclasses in different packages | Class independent in different packages | |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | |
default | Y | Y | ||
private | Y | |||
These four privilege only the emergence of a modifier in any case |
end:
If there are any deficiencies, or content in the wrong place, welcome to give me a shout advice, crab everyone! ^ _ ^
If you can help, then it is to pay attention to me! (Updated series of articles will be the first time the public number)
Here we are strangers, all in the dream and work for their own ❤
Push a stick original Java technology public numbers: more than ten days over two