先放资料:
Learning a New Programming Language: Java for C++ Programmers
java package#
先说几条重要的人话:
- 一个 java 文件第一行可以声明该文件所属于的 package,package 的名字必须与整个工作目录的路径名相同。
- 同一个 package 下的 class 默认有互相访问的权限。
- 访问属性设置为 public 的 class,如果该 class 所在的 file 声明了 package,那么可以被其他 package 下的 class 访问到。
.java的文件名必须与文件中设置为 public 的 class 名保持一致(如果没有 public 的类,那么名称任意)。
- Every class is part of some package.
- All classes in a file are part of the same package.
- You can specify the package using a package declaration (
package name;) as the first (non-comment) line in the file.- Multiple files can specify the same package name.
- If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files).
- If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
- You can access public classes in another (named) package using
package-name.class-name. You can access the public fields and methods of such classes usingpackage-name.class-name.field-or-method-name. You can avoid having to include the package-name usingimport package-name.*;orimport package-name.class-name;at the beginning of the file (after the package declaration). The former imports all of the classes in the package, and the second imports just the named class. You must still useclass-nameto access the classes in the packages, andclass-name.field-or-method-nameto access the fields and methods of the class; the only thing you can leave off is the package name.
下面是一些例子:
Assume that you are working in a directory called Javadir, and that you create four files, whose contents are shown below.
1// file 1 2package ListPkg; 3public class List { ... } 4class ListNode {...} 5 6// file 2 7package ListPkg; 8public class NoNextItemException { ... } 9 10// file 3 11public class Test { ... } 12class Utils { ... } 13 14// file 4 15class Test2 { ... }Here are the directories and file names you must use:
- File 1 must be in a subdirectory named ListPkg, in a file named List.java.
- File 2 must also be in the ListPkg subdirectory, in a file named NoNextItemException.java.
- File 3 must be in a file named Test.java (in the Javadir directory).
- File 4 can be in any .java file (in the Javadir directory).
And here are the classes that can be accessed by the code in each file:
Files 1 and 2:
- The code in the first two files (ListPkg/List.java and ListPkg/NoNextItemException.java) can access the classes defined in the same package (List, ListNode, and NoNextItemException). (No access was specified for those classes, so they get the default, package access.)
- The code in files 1 and 2 cannot access class Test, even though it is a public class. The problem is that Test is in an unnamed package, so the code in the ListPkg package has no way to import that package, or to name class Test.
- The code in files 1 and 2 cannot access classes Utils and Test2, because they have default (package) access, and are in a different package.
Files 3 and 4:
- The code in file 3 (Test.java) can access classes ListPkg.List, ListPkg.NoNextItemException, Test, Utils, and Test2 (the first two because they are public classes in a named package, and the last three because they are in the same, unnamed package, and have either public or package access). Note however, that if the code in Test.java uses the class Test2, and that class is not in a file called Test2.java, then the file that contains class Test2 must be compiled first, or else the class will not be found.
- The code in file 4 (the file that contains class Test2) can access the same classes as the code in file 3 (Test.java).