C#中的命名空间和Java中的包的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9249357/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:32:36  来源:igfitidea点击:

Difference between namespace in C# and package in Java

c#java

提问by Mazzy

What is the difference (in terms of use) between namespaces in C# and packages in Java?

C# 中的命名空间和 Java 中的包之间有什么区别(在使用方面)?

采纳答案by RuudVanNistelrooy

From: http://www.javacamp.org/javavscsharp/namespace.html

来自:http: //www.javacamp.org/javavscsharp/namespace.html



Java

爪哇

Packages are used to organize files or public types to avoid type conflicts. Package constructs can be mapped to a file system.

包用于组织文件或公共类型以避免类型冲突。包结构可以映射到文件系统。

system.security.cryptography.AsymmetricAlgorithm aa;

may be replaced:

可以替换:

import system.security.Crypography; 
class xxx { ...
AsymmetricAlgorithm aa;

There is no alias for packages. You have to use import statement or fully-qualified name to mention the specific type.

包没有别名。您必须使用 import 语句或完全限定名称来提及特定类型。

package n1.n2;
    class A {}
    class B {}

or

或者

package n1.n2;
   class A {}

Another source file:

另一个源文件:

package n1.n2;
   class B {}

Package cannot be nested. One source file can only have one package statement.

包不能嵌套。一个源文件只能有一个包语句。

C#

C#

Namespaces are used to organize programs, both as an "internal" organization system for a program, and as an "external" organization system.

命名空间用于组织程序,既可以作为程序的“内部”组织系统,也可以作为“外部”组织系统。

System.Security.Cryptography.AsymmetricAlgorithm aa;

may be replaced:

可以替换:

using System.Security.Crypography; 
AsymmetricAlgorithm aa;

Alternatively, one could specify an alias for the the namespace, eg

或者,可以为命名空间指定一个别名,例如

using myAlias = System.Security.Crypography; 

and then refer to the class with

然后用

myAlias.AsymmetricAlgorithm 

namespace N1.N2
{
    class A {}
    class B {}
}

or

或者

namespace N1
{
    namespace N2
    {
        class A {}
        class B {}
    }
}

回答by Jon Skeet

There's no such term as "namespace" in Java - a package actsas a namespace in Java though, in terms of providing a scope for names. It's also part of the accessibility model.

Java 中没有“命名空间”这样的术语——虽然包在 Java 中充当命名空间,但就提供名称的范围而言。它也是可访问性模型的一部分。

From section 7 of the Java Language Specification:

来自Java 语言规范的第 7 节

Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A top level type is accessible (§6.6) outside the package that declares it only if the type is declared public.

程序被组织成包的集合。每个包都有自己的一组类型名称,这有助于防止名称冲突。仅当类型声明为 public 时,顶级类型才可在声明它的包外访问(第 6.6 节)。

EDIT: Okay, after the clarification: a Java package is similarto a C# namespace - except that it has an impact on accessibility, whereas in C# namespaces and accessibility are entirely orthogonal.

编辑:好的,澄清之后:Java 包类似于C# 命名空间 - 除了它对可访问性有影响,而在 C# 命名空间和可访问性是完全正交的。

回答by kba

In C++/C#, namespaces are just used for partitioning names to avoid collisions by accidentally using the same name for a variable in different places.

在 C++/C# 中,命名空间仅用于对名称进行分区,以避免在不同位置意外使用相同名称的变量来避免冲突。

In Java, packages are far more than just that - packages are used for modules, the naming aspect is just a part of it.

在 Java 中,包远不止如此——包用于模块,命名方面只是其中的一部分。

回答by Acaz Souza

There are a few details that differ.

有一些细节不同。

In Java the directory structure should match the package structure. No such restriction in C#.

在 Java 中,目录结构应该与包结构相匹配。C# 中没有这样的限制。

In C# you can have multiple namespaces in one file. In Java one file belongs to one package (see previous).

在 C# 中,您可以在一个文件中拥有多个命名空间。在 Java 中,一个文件属于一个包(见前文)。

Java has default/package accessibility. C# internal accessibility goes in assemblies.

Java 具有默认/包可访问性。C# 内部可访问性在程序集中。

If you use VS and Eclipse and let them structure the project, then you will not feel the differences much.

如果你使用VS和Eclipse,让他们来构建项目,那么你不会感觉到太大的差异。

回答by dasumohan89

In java you can apply various access specifiers to classes which will have impact on your packages.

在 java 中,您可以将各种访问说明符应用于将对您的包产生影响的类。

protected : accessible to same package and to its subclasses in another package, default : accessible to same package, public : universally accessible, private : not even accessible by the same package.

protected :可以访问同一个包及其在另一个包中的子类,默认:可以访问同一个包, public :普遍可访问, private :甚至不能被同一个包访问。

These type of access specifiers are not applicable to namespace in c sharp

这些类型的访问说明符不适用于 c 中的命名空间

回答by Rashid Irshad

A namespace is just like a new folder, all subfolders are sub-namespaces. If we consider a namespace as a function like we have a namespace advertising under marketing namespace then we use marketing.advertising.adsclass.adsmethod. Very easy to solve a problem. Java has same method via package but complex for new comers.

命名空间就像一个新文件夹,所有子文件夹都是子命名空间。如果我们将命名空间视为一个函数,就像我们在营销命名空间下有一个命名空间广告一样,那么我们使用 marketing.advertising.adsclass.adsmethod。很容易解决一个问题。Java 通过包具有相同的方法,但对于新手来说很复杂。

In C#

在 C# 中

''' namespace marketing{

''' 命名空间营销{

           class admissions{
                     int admissions_method(){
                     }
           }
     namespace advertising{
           class  advertisement{
                    void ad_in_newspaper( int no_of_lines){
                    }
                    void ad_on_tv(int seconds){
                    }

     }

}

}

To use in client class

在客户端类中使用

using marketing;
using marketing.advertising;

''' In java you use same method. You pack multiple classes in one package and use it many times. It increases uniqueness. You write once and use many times. Related classes in one package. No need to code many times.

''' 在 Java 中,您使用相同的方法。您将多个类打包在一个包中并多次使用。它增加了独特性。您编写一次并使用多次。一个包中的相关类。无需多次编码。