yuantiedizhi:http://translate.google.com.hk/translate?hl=zh-CN&sl=en&tl=zh-CN&u=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNamespace_%28computer_science%29&anno=2
A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.
For example, Bill works for company X and his employee ID is 123. Jane works for company Y and her employee ID is also 123. The reason Bill and Jane can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. There would be serious confusion if the two people worked for the same company, and still had the same employee ID. For instance, a paycheck issued to employee ID 123 would not identify which person should receive the cheque.
In large computer programs or documents it is not uncommon to have hundreds or thousands of identifiers. Namespaces (or a similar technique, see Emulating namespaces ) provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more modular .
Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device so long as they are stored in different directories. In some programming languages (eg. C++ , Python ), the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree . At the root of this tree is the unnamed global namespace .
Contents[hide ]
|
In C++ , a namespace is defined with a namespace block.
namespace
abc {
int
bar;
}
Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of namespace abc
, bar
must be written abc::bar
to be accessed. C++ includes another construct that makes this verbosity unnecessary. By adding the line
using
namespace
abc;
to a piece of code, the prefix abc::
is no longer needed.
Code that is not explicitly declared within a namespace is considered to be in the global namespace.
Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace food::soup
, the identifier chicken
refers to food::soup::chicken
if it exists. If it doesn't exist, it then refers to food::chicken
if it exists. If neither exist, chicken
refers to an identifier in the global namespace.
Namespaces in C++ are most often used to avoid naming collisions . Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire C++ standard library is defined within namespace std
, but before standardization many components were originally in the global namespace .
In Java , the idea of a namespace is embodied in Java packages . All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example class String
in package java.lang
can be referred to as java.lang.String
(this is known as the fully qualified class name ). Like C++, Java offers a construct that makes it unnecessary to type the package name (import
). However, certain features (such as reflection ) require the programmer to use the fully qualified name.
Unlike C++, namespaces in Java are not hierarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages beginning with java
are a part of the Java platform —the package java.lang
contains classes core to the language, and java.lang.reflect
contains core classes specifically relating to reflection.
In Java (and Ada , C# , and others), namespaces/packages express semantic categories of code. For example, in C#, namespace System
.NET Framework ). How specific these categories are and how deep the hierarchies go differ from language to language. contains code provided by the system (the
Function and class scopes can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and object lifetime .
命名空间:主要为了解决命名冲突而引入的一种机制 。