Java “包”和“模块”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3680883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between "package" and "module"?
提问by OscarRyz
I use Java where we only have packages. I know there are other programming languages that also include modules.
我在我们只有包的地方使用 Java。我知道还有其他编程语言也包含模块。
What's the difference?
有什么不同?
采纳答案by T.J. Crowder
It's hard to compare semantics in the void. (What other languages do you mean?) A "module" might be analogous to a Java class, or a Java package, or something else entirely, depending on that other language. Typically since "modules" tend to be from procedural languages, I'd lean toward saying Java class, but I think the line is veryfuzzy at that point and you could argue package quite convincingly.
很难在虚无中比较语义。(您指的是哪些其他语言?)“模块”可能类似于 Java 类、Java 包或其他完全不同的东西,具体取决于其他语言。通常,因为“模块”往往来自过程语言,所以我倾向于说 Java 类,但我认为此时界线非常模糊,您可以非常有说服力地争论包。
回答by user207421
A package is more akin to a C++ namespace than a module. A module is more akin to an enclosing class than to a package.
包比模块更类似于 C++ 命名空间。模块更类似于封闭类而不是包。
回答by missingfaktor
instanceofTom's comment nailed it - Different languages have different definitions of package and module. Therefore there's no language agnostic answer to this question.
instanceofTom 的评论指出了这一点——不同的语言对包和模块有不同的定义。因此,这个问题没有语言不可知的答案。
I'll try to answer it from the perspective of some of the languages I know:
我会尝试从我所知道的一些语言的角度来回答:
Java: It has a concept of
package
s, which are basically just a mechanism for organizing Java classes, interfaces etc into namespaces. They require hierarchical structure.package
s don't have first class status. It might also be worth noting thatsuperpackage
s proposed for Java 7 are also sometimes referred to asmodule
s.Modula:
module
s, same in concept to Java'spackage
s. Don't require hierarchical structure.C#:
namespaces
, same in concept to Java'spackage
s. Don't require hierarchical structure.C++:
namespaces
, As the name indicates, these are just namespaces. Don't require hierarchical structure.Haskell:
module
s, same in concept to Java'spackage
s.Scala:
package
s in Scala are same aspackage
s in Java, except that they don't require hierarchical structures. A few more restrictions like one-public-class-per-file have also been relaxed.object
s in Scala are also referred to as modules, and they also enjoy the first-class status.F#:
namespaces
in F# are same as C#namespace
s. Besidesnamespace
s, F# also hasmodules
which are implemented at CLR level as .NET classes with static methods. They aren't first-class entities.
Java:它有一个
package
s的概念,它基本上只是一种将 Java 类、接口等组织到命名空间中的机制。它们需要层次结构。package
s 没有一流的地位。可能还值得注意的是,superpackage
为 Java 7 提议的 s 有时也称为module
s。Modula:
module
s,与Java的package
s概念相同。不需要层次结构。C#:
namespaces
,在概念上与 Java 的package
s 相同。不需要层次结构。C++:
namespaces
, 顾名思义,这些只是命名空间。不需要层次结构。Haskell:
module
s,与Java 的package
s概念相同。Scala:
package
Scala 中的package
s 与 Java 中的 s相同,只是它们不需要分层结构。还放宽了一些限制,例如每个文件一个公共类。object
Scala 中的 s 也被称为模块,它们也享有一流的地位。F#:
namespaces
在 F# 中与 C# 相同namespace
。除了namespace
s,F# 还具有modules
在 CLR 级别实现为具有静态方法的 .NET 类。他们不是一流的实体。
回答by Josuah Aron
It seems that, as other answers state, the notion of module varies from one language to another, some even not having it (but a corresponding counterpart).
似乎正如其他答案所述,模块的概念因一种语言而异,有些甚至没有(但有相应的对应物)。
However, it seems that nowadays modules is more a concept and refers to a set of source code files that can be shared independently. Following this definition, Java jar files are not modules as they contain bytecode compiled files (.class files). A module system complying with this definition is to be introduced in Java 9 (see this article) and is present in other languages that have module or package managers, often directly naming modules packages(Go lang/go tool, Python/pip, PHP/Composer...) where package generally are packed modules.
然而,现在的模块似乎更多是一个概念,指的是一组可以独立共享的源代码文件。按照这个定义,Java jar 文件不是模块,因为它们包含字节码编译文件(.class 文件)。一个符合这个定义的模块系统将在 Java 9 中引入(参见这篇文章),并且存在于其他具有模块或包管理器的语言中,通常直接命名模块包(Go lang/go tool、Python/pip、PHP/ Composer...) 其中包通常是打包的模块。
回答by Anton Balaniuc
Java Platform Module Systemor JPMS was introduced when java-9was released. Starting from that time Java has both packages
and modules
. So what is the difference between them?
Java 平台模块系统或 JPMS 是在java-9发布时引入的。从那时起,Java 同时具有packages
和modules
. 那么它们之间有什么区别呢?
What is a Package?
什么是包?
A package is a namespace that organizes a set of related classes and interfaces.
包是组织一组相关类和接口的命名空间。
What is a Module?
什么是模块?
A module is a collection of related Java packages and associated resources with a descriptor file, which contains information about which packages/resources are exposed by this module, which packages are used by current module and some other information.
模块是相关 Java 包和相关资源的集合,带有一个描述符文件,该文件包含有关此模块公开哪些包/资源、当前模块使用哪些包以及一些其他信息的信息。
We can thing of a Java Module as a higher level of aggregation above packages. A Module allows you to organize a few packages into one single logical unit and to distribute them as one whole system. As well JPMS provides a way to control which packages are visible to users.
我们可以将 Java 模块视为包之上的更高级别的聚合。模块允许您将几个包组织到一个单一的逻辑单元中,并将它们作为一个完整的系统进行分发。JPMS 还提供了一种方法来控制哪些包对用户可见。