简体   繁体   English

导入不带Java名称的软件包

[英]Import package with no name Java

From http://cs-fundamentals.com/tech-interview/java/which-java-package-is-imported-by-default.php http://cs-fundamentals.com/tech-interview/java/which-java-package-is-imported-by-default.php

In every Java program there can be one unnamed package, which is simply a package with no name. 在每个Java程序中,可以有一个未命名的程序包,它只是一个没有名称的程序包。 If you omit the package statement while writing the class definition, the class name is placed into the default package, which has no name. 如果在编写类定义时省略了package语句,则将类名放入没有名称的默认包中。 Java compiler automatically imports this package. Java编译器会自动导入此软件包。

Is that true? 真的吗?

EDIT 编辑

My concerning about Java compiler automatically imports this package. 我对Java编译器的关注会自动导入此包。

No, the unnamed package is not imported by the compiler. 不,编译器不会导入未命名的程序包。

Please note the following from the Java Language Specification. 请注意Java语言规范中的以下内容。

From Compilation Units : 来自编译单元

A compilation unit that has no package declaration is part of an unnamed package ( §7.4.2 ). 没有包声明的编译单元是未命名包的一部分( 第7.4.2节 )。

From Packages : 来自包装

For small programs and casual development, a package can be unnamed ( §7.4.2 ) or have a simple name, but if code is to be widely distributed, unique package names should be chosen using qualified names . 对于小型程序和临时开发,包可以是未命名的( 第7.4.2节 )或具有简单的名称,但是如果要广泛分发代码, 则应使用限定名称选择唯一的包名称 This can prevent the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single program. 如果两个开发组碰巧选择了相同的程序包名称,而这些程序包后来在单个程序中使用,则可以避免发生冲突。

From Observability of a Package : 包的可观察性

The packages java , java.lang , and java.io are always observable. javajava.langjava.io始终是可观察的。

Note that the unnamed package is not listed . 注意, 未列出未命名的软件包

From Import Declarations : 进口报关单

A type in an unnamed package ( §7.4.2 ) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported , and (b) static members of types in an unnamed package cannot be imported. 未命名包中的类型( 第7.4.2节 )没有规范名称,因此每种类型的导入声明中对规范名称的要求都意味着(a) 无法导入未命名包中的类型 ,以及(b) static成员无法导入未命名包中的类型。 As such, §7.5.1 , §7.5.2 , §7.5.3 , and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package. 因此,第7.5.1节 ,第7.5.2节 ,第7.5.3节和第7.5.4节都要求在未命名的包中导入类型(或其static成员)的任何尝试都产生编译时错误。

No. The statement 不,声明

Java compiler automatically imports this package. Java编译器会自动导入此软件包。

is not correct in several respects. 在几个方面是不正确的。

First of all, packages cannot be imported. 首先,不能导入软件包。 §7.5 (JLS) says: §7.5(JLS)说:

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. 导入声明允许使用由单个标识符组成的简单名称(第6.2节)引用命名类型或静态成员。

This refers to named types and static members, only. 这仅指代命名类型和静态成员。 Not packages! 不包! Although ... there is a convenient method to import every named type from a package with import my.pkg.*; 尽管...有一种方便的方法可以使用import my.pkg.*;从包中导入每个命名类型import my.pkg.*; .

Second, the compiler will not automaticaly import anything from the unnamed package. 其次,编译器不会自动从未命名的包中导入任何内容。 In fact, it is not possible to refer to any program element of the unnamed package from any named package . 实际上, 不可能从任何命名包中引用未命名包的任何程序元素

I guess that this statement means that you have access to all elements in the same package (for which you are writing your code) without any import statements. 我猜想这句话意味着您可以访问同一包(您正在为其编写代码)中的所有元素,而无需任何导入语句。 But this is true for all packages. 但这对于所有软件包都是如此。

Yes. 是。 If you have a project set up in Eclipse, Netbeans, or another IDE, you can attempt to add a class to a project without adding a package. 如果您在Eclipse,Netbeans或其他IDE中设置了项目,则可以尝试将类添加到项目中而不添加包。 It'll usually say something like "Adding classes to the default package is not recommended," but allow you to add that class to the default package anyway. 通常会说类似“不建议将类添加到默认包中”之类的内容,但是无论如何都可以使您将该类添加到默认包中。 Alternatively, you could attempt to run javac on a .java without a package, which would work assuming that all imports, syntax, etc. are correct. 或者,您可以尝试在不带包的.java上运行javac ,并假设所有导入,语法等均正确,这才可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM