您的位置:首页 > 编程语言 > Java开发

Java语言规范基于JavaSE9 第七章 包和模块(七)

2017-12-10 11:29 323 查看
7.6 Top Level Type Declarations

7.6 顶层类型声明

A top level type declaration declares a top level class type (§8 (Classes)) or a top level interface type (§9 (Interfaces)).

顶层类型声明所声明的是顶层类类型(第8章)和顶层接口类型(第9章)。

TypeDeclaration:

 ClassDeclaration

 InterfaceDeclaration

 ;

Extra “;” tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing “;” after a class declaration. They should not be used in new Java code.

在编译单元的顶层类型声明中出现的额外的“;”符号对编译单元的含义不会产生任何影响。在Java编程语言中允许出现单独的分号可以被看做是对习惯了在类声明后面添加“;”的C++程序员的一种妥协。在新的Java代码中不应该使用它们。

In the absence of an access modifier, a top level type has package access: it is accessible only within ordinary compilation units of the package in which it is declared (§6.6.1). A type may be declared public to grant access to the type from code in other packages of the same module, and potentially from code in packages of other modules.

在没有任何访问修饰符时,顶层类型将具有包访问权限:它只能被在声明它的包的普通编译单元内被访问(第6.6.1节)。一个类型也可以被声明为public,以致同一个模块的其他包中的代码可以访问该类型,并且有可能从其他模块的包中的代码访问该类型。

It is a compile-time error if a top level type declaration contains any one of the following access modifiers: protected, private, or static.

如果顶层类型声明包含下列访问修饰符:protected、private或static,那么就是一个编译时错误。

It is a compile-time error if the name of a top level type appears as the name of any other top level class or interface type declared in the same package.

如果顶层类型的名字被当作同一个包中的任何其他顶层类或接口类型的名字,那么就是一个编译时错误。

The scope and shadowing of a top level type is specified in §6.3 and §6.4.

顶层类型的作用域和遮蔽规则在第6.3节和第6.4节中进行了说明。

The fully qualified name of a top level type is specified in §6.7.

顶层类型的完全限定名在第6.7节中进行了说明。

Example 7.6-1. Conflicting Top Level Type Declarations

例7.6-1. 顶层类型声明的冲突

package test;
import java.util.Vector;
class Point {
int x, y;
}
interface Point {   // compile-time error #1
int getR();
int getTheta();
}
class Vector { Point[] pts; }   // compile-time error #2


Here, the first compile-time error is caused by the duplicate declaration of the name Point as both a class and an interface in the same package. A second compile-time error is the attempt to declare the name Vector both by a class type declaration and by a single-type- import declaration.

其中,第一个编译时错误是由于在同一个包中Point名字在一个类和一个接口中进行了重复声明而引起的。第二个编译时错误是由于试图同时通过类类型声明和单类型导入声明来声明Vector名字而引起的。

Note, however, that it is not an error for the name of a class to also name a type that otherwise might be imported by a type-import-on-demand declaration (§7.5.2) in the compilation unit (§7.3) containing the class declaration. Thus, in this program:

但是,注意,类名同时也是类型名并不是错误,该类型可以是在包含该类定义的编译单元(第7.3节)中由按需类型导入声明(第7.5.2节)导入的。因此,在下面的程序中:

package test;
import java.util.*;
class Vector {} // not a compile-time error


the declaration of the class Vector is permitted even though there is also a class java.util.Vector. Within this compilation unit, the simple name Vector refers to the class test.Vector, not to java.util.Vector (which can still be referred to by code within the compilation unit, but only by its fully qualified name).

Vector类的声明是允许的,即使还有一个java.util.Vector类。在该编译单元内,简单名Vector引用的是test.Vector类,而不是java.util.Vector(它仍旧可以被该编译单元内的代码所引用,但是只能通过完全限定名引用)。

Example 7.6-2. Scope of Top Level Types

例7.6-2. 顶层类型的作用域

package points;
class Point {
int x, y;   // coordinates
PointColor color;   // color of this point
Point next; // next point with this color
static int nPoints;
}
class PointColor {
Point first;    // first point with this color
PointColor(int color) { this.color = color; }
private int color;  // color components
}


This program defines two classes that use each other in the declarations of their class members. Because the class types Point and PointColor have all the type declarations in package points, including all those in the current compilation unit, as their scope, this program compiles correctly. That is, forward reference is not a problem.

这个程序定义了两个类,它们在其类成员的声明中彼此使用对方。因为类类型Point和PointColor包含points包中的所有类型声明,包括当前编译单元中的所有类型声明,作为其作用域,所以这个程序可以被正确地编译。也就是说,向前引用不会有问题。

Example 7.6-3. Fully Qualified Names

例7.6-3. 完全限定名

class Point { int x, y; }


In this code, the class Point is declared in a compilation unit with no package declaration, and thus Point is its fully qualified name, whereas in the code:

在这段代码中,Point类是在没有任何package声明的编译单元中声明的,因此Point就是其完全限定名。但是,在下面的代码中:

package vista;
class Point { int x, y; }


the fully qualified name of the class Point is vista.Point. (The package name vista is suitable for local or personal use; if the package were intended to be widely distributed, it would be better to give it a unique package name (§6.1).)

Point类的完全限定名就是vista.Point。(包名vista适合于局部或个人使用。如果想要将这个包广泛分布,那么最好还是给它起一个唯一的包名(第 6.1节)。)

An implementation of the Java SE Platform must keep track of types within packages by the combination of their enclosing module names and their binary names (§13.1). Multiple ways of naming a type must be expanded to binary names to make sure that such names are understood as referring to the same type.

Java SE平台的实现必须通过封装模块名和二进制名(第13.1节)的组合来跟踪包内的类型。命名类型的多种方式必须可以扩展到二进制名,以确保这些名字被理解的类型与其所引用的类型相同。

For example, if a compilation unit contains the single-type-import declaration (§7.5.1):

例如,如果有下面这个包含单类型导入声明(第7.5. 1节)的编译单元:

import java.util.Vector;


then within that compilation unit, the simple name Vector and the fully qualified name

java.util.Vector refer to the same type.

那么在该编译单元内,简单名Vector和完全限定名java.util.Vector引用的是相同的名字。

If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

当且仅当包存储在文件系统中(第7.2节)时,主机系统才可以选择强行限制:在满足下列两个条件之一时,如果在文件名由类型名加扩展名(例如.java或.jav)构成的文件中找不到该类型,那就是一个编译时错误:

• The type is referred to by code in other ordinary compilation units of the package in which the type is declared.

• 该类型被声明该类型的包中的其他普通编译单元中的代码所引用。

• The type is declared public (and therefore is potentially accessible from code in other packages).

• 该类型被声明为public(并因此可以被在其他包中的代码所访问)。

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.

这条限制表明每个编译单元必须至少有一个这种类型。这条限制使得Java编译器可以很容易地找到包中的具名类。实际上,许多开发者选择将每个类或接口类型都置于其自己的编译单元中,无论它是public 的,还是会被其他编译单元中的代码所引用。

For example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

例如,public类型wet.sprocket.Toad的源代码可以在wet/sprocket目录中的Toad.java文件中找到,而相应的对象代码可以在同一个目录下的Toad.class文件中找到。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java