How do I understand "namespaces"? _ Programming

Source: Internet
Author: User

Namespaces are designed to solve the naming problem under the same scope.

In C, C + +, C # and other languages, there is a concept of namespaces, this is to prevent different people write class Library naming conflict design, the namespace can make variables, function names, class names in the space, and other space can use the same name. Just like different folders can have the same file name, but in the same folder cannot have duplicate file name, the namespace is like this virtual folder.

The namespace of the C language. The C language also has this concept, just naming can make the scope of customization very small. In the grammar rules of C language, a number of namespaces are fixed, mainly including:
1, all the tags to form a space, such as Goto statement followed by the label, can not duplicate
2, all the structure, the consortium internal composition of their own independent namespaces, for example, a structure and b structure in the body of the member variable can be the same name, but the same structure does not allow duplicate names
3. The names of all structs, federations, enumerated types constitute the same namespace, and cannot be duplicate, even if the type is different.
4, other variables, functions and other names constitute a namespace, can not duplicate the name.
Therefore, the C language namespace is basically defined by default, and the user-definable part is basically not available.

The namespace of the C++/c# language contains customizable parts that you can define yourself to declare a namespace that uses its own variable names independently in this space, and the namespaces between different users do not conflict.
C + + declares a namespace:

Namespace abc{Code}

Use a namespace in C + +:

using namespace ABC;

Code that uses a space directly in C + +

Abc::func ();        Func () is a function in the space

C # declares a namespace:

Namespace abc{Code}

A namespace is used in C #:

Using ABC;

Using code in a space directly in C #

Abc.func ();         Func () is a function in the space

The concept of "package" in Java is similar to "namespace", can avoid naming conflicts, access control, but in the underlying mechanism is still different, Java package logic structure and the unity of the physical structure, that is, a package corresponding to a folder on disk, the different packets in the object name can naturally be the same, Namespaces are not in the form of such a folder when stored, and Java packages are tied together, and there is no relationship between the packages in which a package is built, and namespaces can be nested.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.