编程中的命名空间是什么?

A namespace is a declarative region for a code block in order to create scope. The namespace is used to organize the code is an elegant and easily readable way.

名称空间是代码块的声明性区域,以便创建作用域。 名称空间用于组织代码是一种优雅且易于阅读的方式。

命名空间用例 (Namespace Usecases)

Namespaces can be used for different cases.

命名空间可用于不同的情况。

Creating New Scope is the most popular use case for the namespaces. Each namespace is a different scope that does not share with other namespaces unless explicitly defined.

Creating New Scope是名称空间最流行的用例。 每个命名空间是一个不同的范围,除非明确定义,否则不会与其他命名空间共享。

Organize Code is another use case where complex projects, applications, and libraries can be categorized and organized properly. This organization can be done according to functions, groups or modules.

Organize Code是另一个用例,可以对复杂的项目,应用程序和库进行适当的分类和组织。 可以根据功能,组或模块来完成此组织。

Provide Limited Accessability is useful for providing namespaces in library for application developers. By using namespaces access to the variables, methods and classes are limited.

Provide Limited Accessability性对于在库中为应用程序开发人员提供名称空间很有用。 通过使用名称空间,可以限制对变量,方法和类的访问。

在C ++中创建和使用命名空间 (Creating and Using Namespace in C++)

C++ uses keyword namespace in order to create a namespace. Namespaces also named in order to access from other scopes or namespaces. All class, methods, variables are put into the specified namespaces. Below we will create a namespace named Poftut and a class named Manager.

C ++使用关键字namespace来创建名称空间。 命名空间也被命名以便从其他作用域或命名空间访问。 所有类,方法,变量都放入指定的名称空间中。 在下面,我们将创建一个名为Poftut的命名空间和一个名为Manager的类。

namespace Poftut
{
class Manager
{
public:
void DoSomething() {}
};
void Func(ObjectManager) {}
}

Below we will use the previously defined namespace Poftut below and create an instance of the class Manager.

下面,我们将在下面使用先前定义的名称空间Poftut并创建类Manager的实例。

using namespace Poftut

Manager mgr;

mgr.DoSomething();

在PHP中创建和使用命名空间 (Creating and Using Namespace in PHP)

PHP is another programming language that supports namespaces. The namespace support came to the PHP with version 5.3. Namespaces generally defined as source code file wide and namespace keyword is used to define.

PHP是另一种支持名称空间的编程语言。 5.3版本PHP提供了对名称空间的支持。 命名空间通常定义为源代码文件范围,而namespace关键字用于定义。

<?php
namespace MyProject;

class Connection{
function connect(){}
}

function test();
?>

In order to use a namespace first, we will import the PHP file which is file1.php in this example. Then simply we will use the defined namespace name and function, class or variable like below. In the following example, we will create an instance of Connection class and call the method test().

为了首先使用命名空间,在此示例中,我们将导入PHP文件file1.php 。 然后,我们将简单地使用定义的名称空间名称和函数,类或变量,如下所示。 在下面的示例中,我们将创建Connection类的实例,然后调用方法test()

<?php
namespace OtherProject;
include 'file1.php';

$mycon = new MyProject\Connection();

MyProject::test();

?>

在C#中创建和使用命名空间 (Creating and Using Namespace in C#)

C# is another programming language that provides namespaces in a very advanced way. Also, C# language, projects, libraries use namespaces heavily. We can create a namespace by using keyword namespace below and put classes, methods, and variables.

C#是另一种以非常高级的方式提供名称空间的编程语言。 同样,C#语言,项目,库大量使用名称空间。 我们可以使用下面的关键字namespace来创建命名namespace ,并放置类,方法和变量。

namespace MyProject
{
class MyClass
{
public void MyMehtod()
{
System.Console.WriteLine(
"SampleMethod inside SampleNamespace");
}
}
}

We will use the MyProject namespace below example and initialize an object and use MyMethod. In order to use a namespace in C# keyword using is used.

我们将在下面的示例中使用MyProject命名空间,并初始化一个对象并使用MyMethod 。 为了使用在C#关键字命名空间using被使用。

using MyProject;

namespace OtherProject
{
class OtherClass
{
static void Main()
{
MyProject.MyClass mc = new MyProject.MyClass();

mc.MyMehtod();
}
}
}
LEARN MORE  C++ Hello World Application
了解更多C ++ Hello World应用程序

翻译自: https://www.poftut.com/what-is-namespace-in-programming/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值