| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
| 技术教程首页 | 开发语言 | WEB开发 | .NET技术 | 数据库 | 操作系统 | 网页制作 |
 
 
您现在的位置: 编程中国 >> 技术教程 >> .NET技术 >> C# >> C#教程 >> 正文
  ►  C#教程第六课:名称空间
C#教程第六课:名称空间
作者:未知    阅读人次:……    文章来源:赢政天下    发布时间:2004-9-13    网友评论()条
 


 

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.");
}
}

 

上一页  [1] [2] [3] [4] 下一页

 

 
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:

  •  
    相关文章
    原创地带
    24小时热门帖子