六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

C#第6課:C#的名稱空間

[摘要]本節課將介紹C#的名稱空間。其目的是: 1.了解什么是名稱空間。 2.了解如何實現"using"指示符。 3.了解"alias" 指示符的用法。 4.了解名稱空間的成員的內容。 在第一課中,你已經在簡單的hello程序中看到了"using Syst...
本節課將介紹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"指示符。一旦你想縮短名稱空間的長名,可以使用別名指示符。另外,除了類之外,你也了解了名稱空間可以使用的其他一些數據類型。


主站蜘蛛池模板: 婷婷六月久久综合丁香可观看 | 网站在线观看高清免费 | 色婷婷5月精品久久久久 | 亚洲人成网址在线观看 | 偷窥自拍亚洲色图 | 欧美一级片免费 | 在线观看 一区二区 麻豆 | 日韩欧美亚洲综合久久影院d3 | 青青青青手机在线观看 | 色香欲综合成人免费视频 | 日本成人免费观看 | 色综合久久久 | 午夜精品aaa国产福利 | 日韩在线操 | 欧美夜色 | 日韩不卡在线观看 | 日韩欧美网 | 欧美性黑人极品hd网站 | 欧美亚洲国产精品久久久久 | 先锋在线资源站 | 青草视频入口 在线观看 | 欧美性视频一区二区三区 | 亚洲人成在线观看男人自拍 | 午夜影院在线观看视频 | 日韩黄色免费观看 | 亚洲六月丁香婷婷综合 | 青青青免费观看在线视频网站 | 青青草手机视频在线观看 | 欧美五月婷婷 | 亚洲欲色| 无人精品乱码一区二区三区 | 色婷婷六月 | 亚洲国产精久久久久久久 | 亚洲高清自拍 | 日本一道在线 | 污污网站国产精品白丝袜 | 中文字幕国产专区 | 色在线网站免费观看 | 四虎影视免费永久在线观看 | 色婷婷久久综合中文久久一本 | 婷婷色爱区综合五月激情韩国 |