“模块” Java 中的功能?
在 Vb.net 中,我们有一个称为“模块”的东西,它提供了一个全局范围,其中模块中的静态函数在整个项目中可见。
Java中有这样的功能吗? (即添加可用于多个包/文件/类的静态方法而无需需要导入。* )
好的我知道这是一个功能会引起很多争论,但实际上这个帖子的重点不是争论这个功能。
In Vb.net, we have something called a "Module" that provides a global scope whereby static functions in the module is visible throughout the entire project.
Is there such a functionality in Java ? (i.e. adding static methods that are available to multiple packages/files/classes without having the need to import .* )
Ok I am aware this is a functionality that would cause alot of debate, but really the point of this thread is not to debate about this functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从字面上回答,不,Java 中没有真正的“全局”。但您不需要导入,特别是
import .*
。下面的代码说明导入不是必需的,但它只是答案的一部分。
在 Java 中,类型(例如类和接口)是根据加载它们的 ClassLoader 来定义的。您可以有多个类加载器,这意味着可以加载同一类的多个副本。其明显的扩展是可以加载具有相同限定名称(包+类)的多个类,并且没有内在要求它们是相关的。
OSGi 就是一个例子,其中每个包都有自己的类加载器,因此您可以同时加载同一类的多个版本。
这与问题有什么关系?那么,如果不能准确地识别您想要引用的类,就不可能准确地识别您想要引用的方法或成员。导入或 FQN 引用可以做到这一点。
To answer literally, no, there's no real "global" in Java. But you don't need to import, particularly not
import .*
.The code below illustrates that an import is not required but it's only really part of the answer.
In Java, types, such as classes and interfaces, are defined in terms of the
ClassLoader
which loaded them. You can have multiple class loaders, which implies that multiple copies of the same class can be loaded. The obvious extension of this is that multiple classes with the same qualified name (package + class) can be loaded, and there's no inherent requirement that they are related.OSGi is an example of this, where each bundle has it's own class loader, and as a result you can have multiple versions of the same class loaded at the same time.
How does this relate to the question? Well, without accurately identifying the class you want to reference it's not possible to accurately identify the method or member you want to reference. The import, or FQN referencing does that.
不,不这么认为。静态导入可以使使用此类方法变得不那么痛苦/冗长,但通常建议小心使用它们。
No, don't think so. Static imports can make it a bit less painful/verbose to use such methods, but it's generally advised to be careful with them.
没有
导入
就不行。您可以声明可从项目中任何位置访问的公共静态方法,但在使用它时仍然必须导入该类或使用完全限定的类路径。Not without the
import
. You can declarepublic static
methods that are accessible from anywhere in the project, but you still have to either import the class or use fully qualified class path when you use it.公共方法在整个项目中都是可见的
,但是如果您想省去导入,您可以使路径明确
public methods are visible throughout the project
however if you want to dispense with the imports you can make the path explicit
不需要。任何静态方法都需要导入才能可见。(这是一件好事)。
正如其他人提到的,Java IDE 为我们完成了这项工作。
No. Import is needed for any static method to be visible.(which is a good thing).
As others mentioned, Java IDEs do this job for us.