A brief introduction to the namespace of Namespace for several common languages

  • 2020-05-09 18:59:20
  • OfStack

Namespaces provide a way to logically organize classes and prevent naming conflicts.

Several common languages

C++

Namespaces can be nested

A nested namespace is a namespace defined in another namespace. The nested namespace is a nested scope, and the name of the inner namespace declaration hides the namesake member of the outer namespace declaration:


int x = 20; 
namespace outer { 
 int x = 10; 
 namespace inner { 
  int z = x; 
 } 
} 
 
int main() 
{ 
 std::cout << outer::inner::z; //  The output 10 
 return 0; 
} 

C#

Nested namespaces

A namespace is declared in a namespace declaration, and each namespace is separated by "."

Such as:


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

Semantically equivalent to


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

Java


package cn.org.web3d.x3dpad

The namespace in Java means that as long as you have a separate top-level domain, you can guarantee the absolute uniqueness of your project.

Objective-C

All class names in Objective-C applications must be globally unique. The name 1 is the Objective-C's hard wound, compared to those elegant languages. Apple's official recommendation for two-letter prefixes for class names is for official libraries and frameworks, and for third-party developers, the official recommendation is to name our classes with three or more letters as prefixes.

PHP


namespace Vendor\Package\.....

It emphasizes that level 1 Vendor should be a one-only identifier, which means you need a top-level domain {Vendor}.com to guarantee the absolute one-only nature of your project. For example, when I think about this point, I immediately register a domain name meanir.com to defend myself.


Related articles: