命名空间
维库,知识与思想的自由文库
|
例如,设Bill是X公司的员工,工号为123,而John是Y公司的员工,工号也是123。由于两人在不同的公司工作,可以使用相同的工号来标识而不会造成混乱,这里每个公司就表示一个独立的命名空间。如果两人在同一家公司工作,其工号就不能相同了,否则在支付工资时便会发生混乱。 这一特点是使用命名空间的主要理由。在大型的计算机程序或文档中,往往会出现数百或数千个标识符。如果不采用命名空间(或类似的方法,见“命名空间的模拟”一节),则很难保证不出现重名的标识符。通过将逻辑上相关的标识符组织成相应的命名空间,可使整个系统更加模块化。 在编程语言中,命名空间是一种特殊的作用域,它包含了处于该作用域内的标识符,且本身也用一个标识符来表示,这样便将一系列在逻辑上相关的标识符用一个标识符组织了起来。许多现代编程语言都支持命名空间。在一些编程语言(例如C++和Python)中,命名空间本身的标识符也属于一个外层的命名空间,也即命名空间可以嵌套,构成一个命名空间树,树根则是无名的全局名空间。 函数和类 scopes can be viewed as implicit namespaces which are inextricably linked with visibility, accessibility, and object lifetime.
[编辑] 在常用编程语言中的应用在C++语言中, 命名空间使用namespace来声明,并使用{ }来界定命名空间的作用域.
namespace foo {
int bar;
}
在这个作用域中, identifiers can be used exactly as they are declared. 在这个作用域外, 使用命名空间内的标识符必须包括命名空间前缀. 比如, 在 using namespace foo; to a piece of code, the prefix Code that is not explicitly declared within a namespace is considered to be in the default namespace. 在C++中,命名空间是可以分层的,即具有层次行. This means that within the hypothetical 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 standard library is defined within In the Java programming language, 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 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 (象其它语言一样,例如Ada, C#)中, 命名空间(或者按照它的习惯称'包(package)')界定了与语义有关代码的作用域. 例如, 在C#中, [编辑] 在XML中的应用XML虽然不是一个独立的编程语言,但是它的出现使得命名空间的使用变得更为广泛。 An XML namespace is a W3C standard for providing uniquely named elements and attributes in an XML instance. An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a namespace then the ambiguity between identically named elements or attributes can be resolved. 在同一个命名空间里,所有的元素名都必须唯一。 A simple example would be to consider an XML instance that contained references to a customer and an ordered product. Both the customer element and the product element could have a child element "ID_number". References to the element ID_number would therefore be ambiguous unless the two identically named but semantically different elements were brought under namespaces that would differentiate them. 声明一个命名空间使用XML保留的属性 The declaration can also include a short prefix with which elements and attributes can be identified e.g. xmlns:xhtml="http://www.w3.org/1999/xhtml". An XML namespace does not require that its vocabulary be defined, though it is fairly common practice to place either a DTD or an XML Schema defining the precise data structure at the location of the namespace's URI. [编辑] 命名空间的模拟In programming languages that do not provide language support for namespaces, namespaces can be emulated to some extent by using an identifier naming convention. For example, C libraries such as Libpng often use a fixed prefix for all functions and variables that are part of their exposed interface. Libpng exposes identifiers such as: png_create_write_struct png_get_signature png_read_row png_set_invalid This gives reasonable assurance that the identifiers are unique and can therefore be used in larger programs without fear of identifier collisions. 但是,这项技术还有一些缺陷:
[编辑] 参见
[编辑] 外部链接 |


