电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> C#>>C#教程第六课:名称空间:

C#教程第六课:名称空间

来源:www.cncfan.com | 2006-12-3 | (有1824人读过)

本节课将介绍C#的名称空间。其目的是:
1.了解什么是名称空间。

2.了解如何实现"using"指示符。

3.了解"alias" 指示符的用法。

4.了解名称空间的成员的内容。

在第一课中,你已经在简单的hello程序中看到了"using System;"指示符的使用。该指示符可以让你使用System名称空间中的成员。在第一课中,未及对此作出详细介绍,现在我们来解释一下名称空间的具体用法。一旦学完了本节课,你将了解"using"指示符及其相关内容。

作为C#的元素,名称空间可以用来帮助组织程序的结构,可以避免两套代码集中命名的冲突。在程序代码中,使用名称空间是个良好的编程习惯,因为这有助于重用你的程序代码。

1.**6-1. The C# Station Namespace: NamespaceCSS.cs

// Namespace Declaration
using System;
// The C# Station Namespace
namespace csharp_station {
// Program start class
class NamespaceCSS {

// Main begins program execution.
public static void Main() {
// Write to console
Console.WriteLine("This is the new C# Station Namespace.");
}
}
}

说明


**6-1演示了如何创建一个名称空间。把单词"namespace"放在"csharp_station"之前,就创建了一个名称空间。"csharp_station"名称空间内的大括号中包含了成员。

2.**6-2 Nested Namespace 1: NestedNamespace1.cs

// Namespace Declaration
using System;
// The C# Station Tutorial Namespace
namespace csharp_station {
namespace tutorial {
// Program start class
class NamespaceCSS {
// Main begins program execution.
public static void Main() {
// Write to console
Console.WriteLine("This is the new C#
Station Tutorial Namespace.");
}
}
}
}

说明

名称空间可以建立一个代码的组织结构。一个良好的编程习惯是:用层次模式来组织你的名称空间。你可以把通用一些的名称放在最顶层,里层则放置一些专用一些的名称。这个层次系统可以用嵌套的名称空间表示。**6-2演示了如何建立一个嵌套的名称空间。在不同的子名称空间内放置代码,从而组织好你的代码的结构。

3.**6-3. Nested Namespace 2: NestedNamespace2.cs

// Namespace Declaration
using System;
// The C# Station Tutorial Namespace
namespace csharp_station.tutorial {
// Program start class
class NamespaceCSS {
// Main begins program execution.
public static void Main() {
// Write to console
Console.WriteLine("This is the new C# Station Tutorial Namespace.");
}
}
}

说明


**6-3演示了另外一种编写嵌套的名称空间的方法。在"csharp_station"和"tutorial"之间置入点运算符,表明这是嵌套的名称空间。结果同**6-2。 相比而言,**6-3 更易书写。

4.**6-4. Calling Namespace Members: NamespaceCall.cs

// Namespace Declaration
using System;
namespace csharp_station {
// nested namespace
namespace tutorial {
class myExample1 {
public static void myPrint1() {
Console.WriteLine("First Example of calling another namespace member.");
}
}
}
// Program start class
class NamespaceCalling {
// Main begins program execution.
public static void Main() {
// Write to console
tutorial.myExample1.myPrint1();
csharp_station.tutorial.myExample2.myPrint2();
}
}
}

// same namespace as nested namespace above
namespace csharp_station.tutorial {
class myExample2 {
public static void myPrint2() {
Console.WriteLine("Second Example of calling another namespace member.");
}
}
}

说明


1.**6-4 的例子演示了用完整的名称指示,调用名称空间的成员。

一个完整的名称指示包括名称空间名,以及调用的方法名。程序的上半部分,在"csharp-station"名称空间内嵌套的名称空间"tutorial"中,定义了类"myExample1"及其方法"myPrint1"。 Main()方法中用完整的名称指示:"tutorial.myExample1.myPrint()" 来进行调用。 因为Main()方法和tutorial名称空间位于同一名称空间内,如果使用"csharp_station"的全称不是必需的。

2.**6-4的下半部分,也是名称空间"csharp_station.tutorial"的一部分。

类"myExample1"和"myExample2"都属于该名称空间。另外,也可以把它们分别写入不同的文件,同时它们仍然属于同一名称空间。在Main()方法中,调用"myPrint2"方法时,采用了全称:"csharp_station.tutorial.myExample2.myPrint2()"。 在这里,必须使用全称中"csharp_station",因为"myExample2"定义在外部。

3.注意:这里对两个不同的类起了不同的名字:

"myExample1"和"myExample2"这是因为对于每个名称空间来说,其中的成员必须有唯一的名称。 记住:它们都处于同一名称空间中,不能取名相同。方法"myPrint1"和"myPrint2" 名称的不同仅仅是为了方便起见,即使同名也没有问题,因为它们属于不同的类。

5.**6-5. The using Directive: UsingDirective.cs

// Namespace Declaration
using System;
using csharp_station.tutorial;
// Program start class
class
UsingDirective {
// Main begins program execution.
public static void Main() {
// Call namespace member
myExample.myPrint();
}
}

// C# Station Tutorial Namespace
namespace csharp_station.tutorial {
class myExample {
public static void myPrint() {
Console.WriteLine("Example of using a using directive.");
}
}

说明

调用方法时,如果你不想打入全称,可使用"using"指示符。在**6-5中,有两个"using"指示符。第一个指示符是"using System",同本教程其它地方出现的"using"指示符相同。你不需要每次都打上"System",只需要打入该名称空间的成员方法名即可。在myPrint()中,"Console"是个"System"名称空间中的成员类,该类有个"WriteLine"的方法。该方法的全称是: "System.Console.WriteLine(...)"。

类似地,using指示符"using csharp_station.tutorial"可以让我们在使用 "csharp_station.tutorial" 名称空间的成员时,无需打入全称。所以,我们可以打入"myExample.myPrint()"。如果不使用"using"指示符,每次实现该方法时,我们就得打入"csharp_station.tutorial.myExample.myPrint()" 。

6.**6-6. The Alias Directive: AliasDirective.cs

// Namespace Declaration
using System;
using csTut = csharp_station.tutorial.myExample; // alias
// Program start class
class AliasDirective {
// Main begins program execution.
public static void Main() {
// Call namespace member
csTut.myPrint();
myPrint();
}
// Potentially ambiguous method.
static void myPrint() {
Console.WriteLine("Not a member of
csharp_station.tutorial.myExample.");
}
}

// C# Station Tutorial Namespace
namespace csharp_station.tutorial {
class myExample {
public static void myPrint() {
Console.WriteLine("This is a member of csharp_station.tutorial.myExample.");
}
}
}

说明

1.有时,往往遇到取名较长的名称空间,而你可以把该名称变短些。

这样就增强了可读性,还避免了同名的冲突。**6-6 演示了如何使用别名指示符,创建别名的格式例子是:"using csTut = csharp_station.tutorial.myExample"。表达式"csTut"可以取代"csharp_station.tutorial.myExample",用在本文件的任何地方。在Main()方法中就使用了"csTut"。

2.在Main()方法中,调用了"AliasDirective" 类中"myPrint" 方法。

这与"myExample" 类的"myPrint"方法同名。 虽然同名,这两个方法都各自正确地进行了调用,原因是:"myExample"类的"myPrint"方法用别名"csTut"表示。编译器能够准确地了解所要执行的是哪个方法。一旦漏掉了"csTut",编译器将两次调用"AliasDirective"类的"myPrint"方法。

3.另外一方面,如果我们没有创建别名指示符,而是添加了"using csharp_station.tutorial.myExample"之后,再调用myPrint(),编译器就会生成出错信息,因为它不知道究竟是调用. "csharp_station.tutorial.myExample.myPrint()"方法呢?还是去调用"AliasDirective.myPrint()"方法。所以使用名称空间是良好的编程习惯,可避免代码中的冲突现象。

小结
到现在为止,我们已经了解在名称空间中可以使用类,实际上,名称空间可以使用如下类型的数据:

类;结构;接口;枚举;代理

在后面的课程中我们将详细介绍这些数据类型。

概括来讲,你已经了解了什么是名称空间,如何定义自己的名称空间。如果你不想打入全称,可以使用"using"指示符。一旦你想缩短名称空间的长名,可以使用别名指示符。另外,除了类之外,你也了解了名称空间可以使用的其他一些数据类型。

C#热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号