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

7.5 Import Declarations

7.5 导入声明

An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.

导入声明使得具名类型或static成员可以用由单个标识符构成的简单名(第6.2节)引用。

Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (§6.7).

如果没有使用恰当的import声明,那么唯一可以引用在另一个包中声明的类型或另一个类型的static成员的方式,就是使用完全限定名(第6.7节)。

ImportDeclaration:
SingleTypeImportDeclaration
TypeImportOnDemandDeclaration
SingleStaticImportDeclaration
StaticImportOnDemandDeclaration

• A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).

• 单类型导入声明(第7.5.1节)可以通过提及单个具名类型的规范名(第6.7节)而导入它。

• A type-import-on-demand declaration (§7.5.2) imports all the accessible types of a named type or named package as needed, by mentioning the canonical name of a type or package.

• 按需类型导入声明(第7.5.2节)可以通过提及类型或包的规范名而导入具名类型或具名包中所有需要的可访问类型。

• A single-static-import declaration (§7.5.3) imports all accessible static
members with a given name from a type, by giving its canonical name.

• 单静态导入声明(第7.5.3节)可以通过给定规范名而导入类型中给定名字的所有可访问的static成员。

• A static-import-on-demand declaration (§7.5.4) imports all accessible static
members of a named type as needed, by mentioning the canonical name of a type.

• 按需静态导入声明(第7.5.4节)可以通过提及类型的规范名而导入具名类型中所有需要的可访问static成员。

The scope and shadowing of a type or member imported by these declarations is specified in §6.3 and §6.4.

这些声明导入的类型或成员的作用域与遮蔽规则在第6.3节和第6.4节中进行了说明。

An import declaration makes types or members available by their simple names only within the compilation unit that actually contains the import declaration. The scope of the type(s) or member(s) introduced by an import declaration specifically does not include other compilation units in the same package, other import declarations in the current compilation unit, or a package declaration in the current compilation unit (except for the annotations of a package declaration).

import声明使得在实际包含该import声明的编译单元中只需要使用简单名就可获得类或成员。由import声明引入的类型或成员的作用域并不包含在同一个包中声明的其他编译单元、在当前编译单元中的其他 import声明,或当前编译单元中的package声明(除了package声明的注解)。

7.5.1 Single-Type-Import Declarations

7.5.1 单类型导入声明

A single-type-import declaration imports a single type by giving its canonical name, making it available under a simple name in the module, class, and interface declarations of the compilation unit in which the single-type-import declaration appears.

单类型导入声明通过给定类型的规范名而导入了单个类型,使其在该单类型导入声明所在的编译单元的模块、类或接口声明中使用简单名即可获得它们。

SingleTypeImportDeclaration:
import TypeName ;

The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type (§6.7).

TypeName必须是类类型、接口类型、枚举类型或注解类型的规范名(第6.7节)。

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.

该类型必须是具名包的成员;或者是最外层的封闭类型声明(第8.1.3节)是具名包的成员的类型的成员;或者是发生编译时错误的类型的成员。

It is a compile-time error if the named type is not accessible (§6.6).

如果具名类型不可访问(第6.6节),那么这就是一个编译时错误。

If two single-type-import declarations in the same compilation unit attempt to import types with the same simple name, then a compile-time error occurs, unless the two types are the same type, in which case the duplicate declaration is ignored.

如果在同一个编译单元中的两个单类型导入声明试图导入具有相同简单名的类型,那么就会产生编译时错误,除非这两种类型是同一种类型,在这种情况下,重复声明将被忽略。

If the type imported by the single-type-import declaration is declared in the compilation unit that contains the import declaration, the import declaration is ignored.

如果由单类型导入声明导入的类型是在包含该import声明的编译单元中声明的,那么该import声明就会被忽略。

If a single-type-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type (§7.6) whose simple name is n, a compile-time error occurs.

如果单类型导入声明导入的类型的简单名是n,并且编译单元也声明了简单名为n的顶层类型(第7.6节),那么就会产生编译时错误。

If a compilation unit contains both a single-type-import declaration that imports a type whose simple name is n, and a single-static-import declaration (§7.5.3) that imports a type whose simple name is n, a compile-time error occurs.

如果编译单元中包含导入简单名为n的类型的单类型导入声明和导入简单名为n的单静态导入声明(第7.5.3节),那么就会产生编译时错误。

Example 7.5.1-1. Single-Type-Import

例7.5.1-1. 单类型导入

import java.util.Vector;

causes the simple name Vector to be available within the class and interface declarations in a compilation unit. Thus, the simple name Vector refers to the type declaration Vector in the package java.util in all places where it is not shadowed (§6.4.1) or obscured (§6.4.2) by a declaration of a field, parameter, local variable, or nested type declaration with the same name.

上面的代码将使得简单名Vector在某个编译单元中的类和接口中是可用的。因此,在任何没有被具有相同名字的域、参数、局部变量声明,或嵌套类型声明所遮蔽(第6.4.1节)或遮掩(第6.4.2节)的地方,简单名Vector都将引用java.util包中的Vector的类型声明。

Note that the actual declaration of java.util.Vector is generic (§8.1.2). Once imported, the name Vector can be used without qualification in a parameterized type such as Vector, or as the raw type Vector. A related limitation of the import declaration is that a nested type declared inside a generic type declaration can be imported, but its outer type is always erased.

注意java.util.Vector被声明为泛型(第8.1.2节)。一旦被导入,Vector这个名字就可以在参数化类型中不经限定而使用,例如Vector <String>,或者当作原生类型Vector使用。这里要强调import声明的限制:在泛型声明内部嵌套的类型可以被导入,但是其外部类总是会被擦除。

Example 7.5.1-2. Duplicate Type Declarations

例7.5.1-2. 重复类型导入

This program:

下面这个程序:

import java.util.Vector;
class Vector { Object[] vec; }

causes a compile-time error because of the duplicate declaration of Vector, as does:

将会导致编译时错误,因为Vector的声明重复,下面代码的情况也是如此:

import java.util.Vector; 
import myVector.Vector;

where myVector is a package containing the compilation unit:

其中myVector是包含下面的编译单元的包:

package myVector;
public class Vector { Object[] vec; }

Example 7.5.1-3. No Import of a Subpackage

例7.5.1-3. 不导入子包

Note that an import declaration cannot import a subpackage, only a type.

注意,import声明不能导入子包,只能导入类型。

For example, it does not work to try to import java.util and then use the name util.Random to refer to the type java.util.Random:

例如,试图导入java.util,然后使用util.Random这个名字来引用java.util.Random类型是行不通的。

import java.util;
class Test { util.Random generator; }
// incorrect: compile-time error

Example 7.5.1-4. Importing a Type Name that is also a Package Name

例7.5.1-4. 导入也是包名的类型名

Package names and type names are usually different under the naming conventions described in §6.1. Nevertheless, in a contrived example where there is an unconventionally- named package Vector, which declares a public class whose name is Mosquito:

包名和类型名在第6.1节的命名惯用法下通常是不同的。尽管如此,在下面这个刻意设计的示例中,有一个不按照惯用法命名的包Vector,它声明了名字为Mosquito的公共类 :

package Vector;
public class Mosquito { int capacity; }

and then the compilation unit:

这样,对于下面的编译单元:

package strange;
import java.util.Vector; 
import Vector.Mosquito; 
class Test {
    public static void main(String[] args) {
        System.out.println(new Vector().getClass());
        System.out.println(new Mosquito().getClass());
    }
}

the single-type-import declaration importing class Vector from package java.util does not prevent the package name Vector from appearing and being correctly recognized in subsequent import declarations. The example compiles and produces the output:

从java.util包中导入Vector类的单类型导入声明没有妨碍包名Vector在随后的import声明中出现和被正确识别。这个示例可以编译,并会产生下面的输出:

class java.util.Vector
class Vector.Mosquito

7.5.2 Type-Import-on-Demand Declarations

7.5.2 按需类型导入声明

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

按需导入类型声明允许具名包或类型的所有可访问类型都可以按需导入。

TypeImportOnDemandDeclaration:
import PackageOrTypeName . * ;

The PackageOrTypeName must be the canonical name (§6.7) of a package, a class type, an interface type, an enum type, or an annotation type.

PackageOrTypeName必须是包、类类型、接口类型、枚举类型或注解类型的规范名(第6.7节)。

If the PackageOrTypeName denotes a type (§6.5.4), then the type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type declaration (§8.1.3) is a member of a named package, or a compile- time error occurs.

如果PackageOrTypeName表示的是类型(第6.5.4节),那么该类型必须是具名包的成员;或者是最外层的封闭类型声明(第8.1.3节)是具名包的成员的类型的成员;或者是发生编译时错误的类型的成员。

It is a compile-time error if the named package is not uniquely visible to the current module (§7.4.3), or if the named type is not accessible (§6.6).

如果具名包不是当前模块唯一可见的(第7.4.3节),或者具名类型是不可访问的(第6.6节),则会发生编译时错误。

It is not a compile-time error to name either java.lang or the named package of the current compilation unit in a type-import-on-demand declaration. The type-import- on-demand declaration is ignored in such cases.

如果在按需类型导入声明中命名的是java.lang或者是当前编译单元的具名包,则不算是编译时错误。在这种情况下,按需类型导入声明会被忽略。

Two or more type-import-on-demand declarations in the same compilation unit may name the same type or package. All but one of these declarations are considered redundant; the effect is as if that type was imported only once.

在同一个编译单元中两个或更多的按需类型导入声明可以命名相同的类型或包,但是除了其中的一个声明之外,其他的声明都被认为是冗余的,其效果就像是该类型只被导入了一次。

If a compilation unit contains both a type-import-on-demand declaration and a static-import-on-demand declaration (§7.5.4) that name the same type, the effect is as if the static member types of that type (§8.5, §9.5) were imported only once.

在同时包含命名了相同类型的按需类型导入声明和按需静态导入类型声明(第7.5.4节)的编译单元中,其效果就像是该类型的static成员(第8.5节、第9.5节)只被导入了一次。

Example 7.5.2-1. Type-Import-on-Demand

例7.5.2-1 按需类型导入

import java.util.*;

causes the simple names of all public types declared in the package java.util to be available within the class and interface declarations of the compilation unit. Thus, the simple name Vector refers to the type Vector in the package java.util in all places in the compilation unit where that type declaration is not shadowed (§6.4.1) or obscured (§6.4.2).

上面的代码会导致在java.util包中声明的所有public类型的简单名在该编译单元的类和接口声明内都是可用的。因此,简单名Vector在该编译单元中引用的应该是java.util包中的Vector类型,前提是该类型声明没有被遮蔽(第6.4.1节)或遮掩(第6.4.2节 )。

The declaration might be shadowed by a single-type-import declaration of a type whose simple name is Vector; by a type named Vector and declared in the package to which the compilation unit belongs; or any nested classes or interfaces.

该声明可能被简单名是Vector的类型的单类型导入声明所遮蔽,或者被在该编译单元所属的包中声明的命名为Vector的类型所遮蔽,或者被任何嵌套类或接口所遮蔽。

The declaration might be obscured by a declaration of a field, parameter, or local variable named Vector.

该声明可能被命名为Vector的域、参数或局部变量所遮掩。

(It would be unusual for any of these conditions to occur.)

(上述情况都属于不寻常情况。)

7.5.3 Single-Static-Import Declarations

7.5.3 单静态导入声明

A single-static-import declaration imports all accessible static members with a given simple name from a type. This makes these static members available under their simple name in the module, class, and interface declarations of the compilation unit in which the single-static-import declaration appears.

单静态导入声明从某个类型中导入了具有给定简单名的所有可访问的static成员。这使得这些static成员在该单静态导入声明所在的编译单元的模块、类或接口声明中使用简单名即可获得它们。

SingleStaticImportDeclaration:
import static TypeName . Identifier ;

The TypeName must be the canonical name (§6.7) of a class type, interface type, enum type, or annotation type.

TypeName必须是类类型、接口类型、枚举类型或注解类型的规范名(第6.7节)。

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.

该类型必须是具名包的成员;或者是最外层的封闭类型声明(第8.1.3节)是具名包的成员的类型的成员;或者是发生编译时错误的类型的成员。

It is a compile-time error if the named type is not accessible (§6.6).

如果具名类型不可访问(第6.6节),则会产生编译时错误。

The Identifier must name at least one static member of the named type. It is a compile-time error if there is no static member of that name, or if all of the named members are not accessible.

Identifier必须至少命名该具名类型的一个static成员。如果没有任何具有该名字的static成员,或者所有具有该名字的成员都是不可访问的,则会产生编译时错误 。

It is permissible for one single-static-import declaration to import several fields or types with the same name, or several methods with the same name and signature. This occurs when the named type inherits multiple fields, member types, or methods, all with the same name, from its own supertypes.

静态导入声明导入具有相同名字的多个域或类型,或者具有相同名字和签名的多个方法是允许的。这种情况发生在指定类型继承自其超类型的多个字段、成员类型或方法全部使用相同名称时。

If a single-static-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type (§7.6) whose simple name is n, a compile-time error occurs.

如果单静态导入声明导入的类型的简单名是n,而该编译单元也命名了简单名为n的顶层类型(第7.6节),则会产生编译时错误。

If a compilation unit contains both a single-static-import declaration that imports a type whose simple name is n, and a single-type-import declaration (§7.5.1) that imports a type whose simple name is n, a compile-time error occurs.

如果在一个编译单元中,同时包含导入简单名为n的类型的单静态导入声明和导入简单名为n的类型的单类型导入声明(第7.5.1节),则会产生编译时错误。

7.5.4 Static-Import-on-Demand Declarations

7.5.4 按需静态导入声明

A static-import-on-demand declaration allows all accessible static members of a named type to be imported as needed.

按需静态导入声明允许具名类型的所有可访问static成员都可以按需导入。

StaticImportOnDemandDeclaration:
import static TypeName . ;*

The TypeName must be the canonical name (§6.7) of a class type, interface type, enum type, or annotation type.

TypeName必须是类类型、接口类型、枚举类型或注解类型的规范名(第6.7节)。

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type declaration (§8.1.3) is a member of a named package, or a compile-time error occurs.

该类型必须是具名包的成员;或者是最外层的封闭类型声明(第8.1.3节)是具名包的成员的类型的成员;或者是发生编译时错误的类型的成员。

It is a compile-time error if the named type is not accessible (§6.6).

如果具名类型不可访问(第6.6节),则会产生编译时错误。

Two or more static-import-on-demand declarations in the same compilation unit may name the same type; the effect is as if there was exactly one such declaration.

在同一个编译单元中两个或更多的按需静态导入声明可以命名相同的类型,其效果就像是只有一个这种声明一样。

Two or more static-import-on-demand declarations in the same compilation unit may name the same member; the effect is as if the member was imported exactly once.

在同一个编译单元中两个或更多的按需类型导入声明可以命名相同的成员,其效果就像是该成员只被导入了一次。

It is permissible for one static-import-on-demand declaration to import several fields or types with the same name, or several methods with the same name and signature. This occurs when the named type inherits multiple fields, member types, or methods, all with the same name, from its own supertypes.

按需静态导入声明导入具有相同名字的多个域或类型,或者具有相同名字和签名的多个方法是允许的。这种情况发生在指定类型继承自其超类型的多个字段、成员类型或方法全部使用相同名称时。

If a compilation unit contains both a static-import-on-demand declaration and a type-import-on-demand declaration (§7.5.2) that name the same type, the effect is as if the static member types of that type (§8.5, §9.5) were imported only once.

如果在一个编译单元中,同时包含命名相同类型的按需静态导入声明和按需类型导入声明(第7.5.2节),那么其效果就像是该类型(第8.5节、第9.5节)的static成员类型只被导入了一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值