如果 Java 没有预处理器,那么“导入”是什么?

发布于 2024-12-15 02:17:12 字数 181 浏览 3 评论 0原文

这篇文章说 2.2 中不再有类型定义、定义或预处理器.1.在 C++ 中,include 是预处理器的一部分。什么是导入

This article says No More Typedefs, Defines, or Preprocessor in 2.2.1 . In C++ the include is part of the preprocessor. What is the import?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

触ぅ动初心 2024-12-22 02:17:13

尽管名称不会“导入”任何内容,但导入只是让您可以在没有完全限定名称的情况下调用类。

澄清一下,如果我执行 import java.util.ArrayList;,现在我可以将 ArrayList 类引用为 ArrayList。如果我不这样做,我仍然可以使用该类,只需将其命名为java.util.ArrayList

如果你用 * 导入整个包,最糟糕的情况就是发生名称冲突,因此,你必须使用全名来引用 Java 类,但事实并非如此。运行时不要使用更多内存。

java.lang 中的类会自动“导入”。

Java 1.5 引入了静态导入,使程序员能够引用导入的静态成员,就像它们是在使用它们的类中声明的一样。应谨慎使用它们。可接受的用途是导入 JUnit Assert 方法。例如,使用传统导入:

import org.junit.Assert;
...
Assert.assertEquals(expected, actual);

使用静态导入:

import static org.junit.Assert.assertEquals;
...
assertEquals(expected, actual);

Import despite the name doesn't "import" anything, it just let you call the classes without the fully qualified name.

To clarify, if I do an import java.util.ArrayList;, now I can refer to ArrayList class as just ArrayList. If I don't do it, I can still use the class, I just have to call it java.util.ArrayList.

If you import whole packages with *, the worst thing it can happen is that there is a name clash, thus, you've to use the full name to refer to a Java class, but it doesn't use more memory at runtime.

Classes in java.lang are automatically "imported".

Java 1.5 introduced static imports, which enables programmers to refer to imported static members as if they were declared in the class that uses them. They should be used sparingly. An acceptable use is for importing JUnit Assert methods. For instance, with a traditional import:

import org.junit.Assert;
...
Assert.assertEquals(expected, actual);

With static import:

import static org.junit.Assert.assertEquals;
...
assertEquals(expected, actual);
匿名的好友 2024-12-22 02:17:13

导入允许您使用非限定类名。例如,通过 import java.util.ArrayList,您可以在代码中使用非限定类型名称 ArrayList。如果没有 import 语句,您必须始终使用完全限定名称:java.util.ArrayList

还有静态导入,它将静态类元素引入编译单元的名称空间。

Import allows you to use an unqualified class name. For instance, with import java.util.ArrayList you can use an unqualified type name ArrayList in your code. Without the import statement, you would have to always use the fully qualified name: java.util.ArrayList.

There's also static import, which brings static class elements into the name space of the compilation unit.

公布 2024-12-22 02:17:13

import 使包名称在使用它的文件中已知。它根本无法与C 的#inlinclude 相提并论。

import makes package names known within the file where it is used. It is not at all comparable to C's #inlclude.

月竹挽风 2024-12-22 02:17:13

由于严格的源代码命名约定,Java 编译器只需从包和类的完全限定名称即可轻松找到相应的源文件或类文件。通过完全限定名称,我的意思是指定完整的包和类,例如,

java.util.ArrayList x = new java.util.ArrayList ();

这种冗长编码风格的替代方法是使用 import 语句。

import java.io.*;
import java.util.ArrayList;
ArrayList x = new java.util.ArrayList();

对于理解别人的代码也有很大的帮助

Because of the rigid source code naming convention, the Java compiler can easily find the corresponding source or class files just from the fully qualified name of a package and class. By fully qualified name I mean specifying the full package and class e.g.

java.util.ArrayList x = new java.util.ArrayList ();

The alternative to this long-winded style of coding, is to use import statements.

import java.io.*;
import java.util.ArrayList;
ArrayList x = new java.util.ArrayList();

Also it is a great help to understanding someone else’s code

酒几许 2024-12-22 02:17:13

有一种方法可以告诉编译器您正在使用另一个包中的某个类吗?

编辑:规范链接

A way to tell the compiler you are using some class from another package?

EDIT: link to the spec

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文