development

C #의 네임 스페이스와 Java의 패키지의 차이점

big-blog 2020. 9. 3. 23:28
반응형

C #의 네임 스페이스와 Java의 패키지의 차이점


C #의 네임 스페이스와 Java의 패키지 간의 차이점 (사용 측면에서)은 무엇입니까?


출처 : http://www.javacamp.org/javavscsharp/namespace.html


자바

패키지는 유형 충돌을 피하기 위해 파일 또는 공용 유형을 구성하는 데 사용됩니다. 패키지 구성은 파일 시스템에 매핑 될 수 있습니다.

system.security.cryptography.AsymmetricAlgorithm aa;

대체 가능 :

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

패키지에 대한 별칭이 없습니다. 특정 유형을 언급하려면 import 문 또는 정규화 된 이름을 사용해야합니다.

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

또는

package n1.n2;
   class A {}

다른 소스 파일 :

package n1.n2;
   class B {}

패키지는 중첩 될 수 없습니다. 하나의 소스 파일에는 하나의 패키지 문만있을 수 있습니다.

씨#

네임 스페이스는 프로그램의 "내부"조직 시스템 및 "외부"조직 시스템으로 프로그램을 구성하는 데 사용됩니다.

System.Security.Cryptography.AsymmetricAlgorithm aa;

대체 가능 :

using System.Security.Crypography; 
AsymmetricAlgorithm aa;

또는 네임 스페이스에 대한 별칭을 지정할 수 있습니다.

using myAlias = System.Security.Crypography; 

다음으로 수업을 참조하십시오.

myAlias.AsymmetricAlgorithm 

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

또는

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

There are a few details that differ.

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

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

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

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


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

From section 7 of the Java Language Specification:

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.

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


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.

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


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

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.

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


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.

In 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.

참고URL : https://stackoverflow.com/questions/9249357/difference-between-namespace-in-c-sharp-and-package-in-java

반응형