C#教程第六課:C#的名稱空間
2024-07-21 02:22:17
供稿:網友
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。本節課將介紹c#的名稱空間。其目的是: <br>
1.了解什么是名稱空間。 <br>
<br>
2.了解如何實現"using"指示符。 <br>
<br>
3.了解"alias" 指示符的用法。 <br>
<br>
4.了解名稱空間的成員的內容。 <br>
<br>
在第一課中,你已經在簡單的hello程序中看到了"using system;"指示符的使用。該指示符可以讓你使用system名稱空間中的成員。在第一課中,未及對此作出詳細介紹,現在我們來解釋一下名稱空間的具體用法。一旦學完了本節課,你將了解"using"指示符及其相關內容。 <br>
<br>
作為c#的元素,名稱空間可以用來幫助組織程序的結構,可以避免兩套代碼集中命名的沖突。在程序代碼中,使用名稱空間是個良好的編程習慣,因為這有助于重用你的程序代碼。 <br>
<br>
1.清單6-1. the c# station namespace: namespacecss.cs <br>
<br>
// namespace declaration<br>
using system;<br>
// the c# station namespace<br>
namespace csharp_station {<br>
// program start class<br>
class namespacecss {<br>
<br>
// main begins program execution.<br>
public static void main() {<br>
// write to console<br>
console.writeline("this is the new c# station namespace."); <br>
}<br>
}<br>
} <br>
<br>
說明 <br>
<br>
清單6-1演示了如何創建一個名稱空間。把單詞"namespace"放在"csharp_station"之前,就創建了一個名稱空間。"csharp_station"名稱空間內的大括號中包含了成員。 <br>
<br>
2.清單6-2 nested namespace 1: nestednamespace1.cs <br>
<br>
// namespace declaration<br>
using system;<br>
// the c# station tutorial namespace<br>
namespace csharp_station {<br>
namespace tutorial {<br>
// program start class<br>
class namespacecss {<br>
// main begins program execution.<br>
public static void main() {<br>
// write to console<br>
console.writeline("this is the new c# <br>
station tutorial namespace."); <br>
}<br>
}<br>
}<br>
} <br>
<br>
說明 <br>
<br>
名稱空間可以建立一個代碼的組織結構。一個良好的編程習慣是:用層次模式來組織你的名稱空間。你可以把通用一些的名稱放在最頂層,里層則放置一些專用一些的名稱。這個層次系統可以用嵌套的名稱空間表示。清單6-2演示了如何建立一個嵌套的名稱空間。在不同的子名稱空間內放置代碼,從而組織好你的代碼的結構。 <br>
<br>
3.清單6-3. nested namespace 2: nestednamespace2.cs <br>
<br>
// namespace declaration<br>
using system;<br>
// the c# station tutorial namespace<br>
namespace csharp_station.tutorial {<br>
// program start class<br>
class namespacecss {<br>
// main begins program execution.<br>
public static void main() {<br>
// write to console<br>
console.writeline("this is the new c# station tutorial namespace."); <br>
}<br>
}<br>
} <br>
<br>
說明 <br>
<br>
清單6-3演示了另外一種編寫嵌套的名稱空間的方法。在"csharp_station"和"tutorial"之間置入點運算符,表明這是嵌套的名稱空間。結果同清單6-2。 相比而言,清單6-3 更易書寫。 <br>
<br>
4.清單6-4. calling namespace members: namespacecall.cs <br>
<br>
// namespace declaration<br>
using system;<br>
namespace csharp_station {<br>
// nested namespace<br>
namespace tutorial {<br>
class myexample1 {<br>
public static void myprint1() {<br>
console.writeline("first example of calling another namespace member.");<br>
}<br>
}<br>
}<br>
// program start class<br>
class namespacecalling {<br>
// main begins program execution.<br>
public static void main() {<br>
// write to console<br>
tutorial.myexample1.myprint1(); <br>
csharp_station.tutorial.myexample2.myprint2(); <br>
}<br>
}<br>
}<br>
<br>
// same namespace as nested namespace above<br>
namespace csharp_station.tutorial {<br>
class myexample2 {<br>
public static void myprint2() {<br>
console.writeline("second example of calling another namespace member.");<br>
}<br>
}<br>
} <br>
<br>
說明 <br>
<br>
1.清單6-4 的例子演示了用完整的名稱指示,調用名稱空間的成員。 <br>
<br>
一個完整的名稱指示包括名稱空間名,以及調用的方法名。程序的上半部分,在"csharp-station"名稱空間內嵌套的名稱空間"tutorial"中,定義了類"myexample1"及其方法"myprint1"。 main()方法中用完整的名稱指示:"tutorial.myexample1.myprint()" 來進行調用。 因為main()方法和tutorial名稱空間位于同一名稱空間內,如果使用"csharp_station"的全稱不是必需的。 <br>
<br>
2.清單6-4的下半部分,也是名稱空間"csharp_station.tutorial"的一部分。 <br>
<br>
類"myexample1"和"myexample2"都屬于該名稱空間。另外,也可以把它們分別寫入不同的文件,同時它們仍然屬于同一名稱空間。在main()方法中,調用"myprint2"方法時,采用了全稱:"csharp_station.tutorial.myexample2.myprint2()"。 在這里,必須使用全稱中"csharp_station",因為"myexample2"定義在外部。 <br>
<br>
3.注意:這里對兩個不同的類起了不同的名字: <br>
<br>
"myexample1"和"myexample2"這是因為對于每個名稱空間來說,其中的成員必須有唯一的名稱。 記住:它們都處于同一名稱空間中,不能取名相同。方法"myprint1"和"myprint2" 名稱的不同僅僅是為了方便起見,即使同名也沒有問題,因為它們屬于不同的類。 <br>
<br>
5.清單6-5. the using directive: usingdirective.cs <br>
<br>
// namespace declaration<br>
using system;<br>
using csharp_station.tutorial;<br>
// program start class<br>
class<br>
usingdirective {<br>
// main begins program execution.<br>
public static void main() {<br>
// call namespace member<br>
myexample.myprint(); <br>
}<br>
}<br>
<br>
// c# station tutorial namespace<br>
namespace csharp_station.tutorial {<br>
class myexample {<br>
public static void myprint() {<br>
console.writeline("example of using a using directive.");<br>
}<br>
} <br>
<br>
說明 <br>
<br>
調用方法時,如果你不想打入全稱,可使用"using"指示符。在清單6-5中,有兩個"using"指示符。第一個指示符是"using system",同本教程其它地方出現的"using"指示符相同。你不需要每次都打上"system",只需要打入該名稱空間的成員方法名即可。在myprint()中,"console"是個"system"名稱空間中的成員類,該類有個"writeline"的方法。該方法的全稱是: "system.console.writeline(...)"。 <br>
<br>
類似地,using指示符"using csharp_station.tutorial"可以讓我們在使用 "csharp_station.tutorial" 名稱空間的成員時,無需打入全稱。所以,我們可以打入"myexample.myprint()"。如果不使用"using"指示符,每次實現該方法時,我們就得打入"csharp_station.tutorial.myexample.myprint()" 。 <br>
<br>
6.清單6-6. the alias directive: aliasdirective.cs <br>
<br>
// namespace declaration<br>
using system;<br>
using cstut = csharp_station.tutorial.myexample; // alias<br>
// program start class<br>
class aliasdirective {<br>
// main begins program execution.<br>
public static void main() {<br>
// call namespace member<br>
cstut.myprint();<br>
myprint();<br>
}<br>
// potentially ambiguous method.<br>
static void myprint() {<br>
console.writeline("not a member of <br>
csharp_station.tutorial.myexample.");<br>
}<br>
}<br>
<br>
// c# station tutorial namespace<br>
namespace csharp_station.tutorial {<br>
class myexample {<br>
public static void myprint() {<br>
console.writeline("this is a member of csharp_station.tutorial.myexample.");<br>
}<br>
}<br>
} <br>
<br>
說明 <br>
<br>
1.有時,往往遇到取名較長的名稱空間,而你可以把該名稱變短些。 <br>
<br>
這樣就增強了可讀性,還避免了同名的沖突。清單6-6 演示了如何使用別名指示符,創建別名的格式例子是:"using cstut = csharp_station.tutorial.myexample"。表達式"cstut"可以取代"csharp_station.tutorial.myexample",用在本文件的任何地方。在main()方法中就使用了"cstut"。 <br>
<br>
2.在main()方法中,調用了"aliasdirective" 類中"myprint" 方法。 <br>
<br>
這與"myexample" 類的"myprint"方法同名。 雖然同名,這兩個方法都各自正確地進行了調用,原因是:"myexample"類的"myprint"方法用別名"cstut"表示。編譯器能夠準確地了解所要執行的是哪個方法。一旦漏掉了"cstut",編譯器將兩次調用"aliasdirective"類的"myprint"方法。 <br>
<br>
3.另外一方面,如果我們沒有創建別名指示符,而是添加了"using csharp_station.tutorial.myexample"之后,再調用myprint(),編譯器就會生成出錯信息,因為它不知道究竟是調用. "csharp_station.tutorial.myexample.myprint()"方法呢?還是去調用"aliasdirective.myprint()"方法。所以使用名稱空間是良好的編程習慣,可避免代碼中的沖突現象。 <br>
<br>
小結 <br>
到現在為止,我們已經了解在名稱空間中可以使用類,實際上,名稱空間可以使用如下類型的數據: <br>
<br>
類;結構;接口;枚舉;代理 <br>
<br>
在后面的課程中我們將詳細介紹這些數據類型。 <br>
<br>
概括來講,你已經了解了什么是名稱空間,如何定義自己的名稱空間。如果你不想打入全稱,可以使用"using"指示符。一旦你想縮短名稱空間的長名,可以使用別名指示符。另外,除了類之外,你也了解了名稱空間可以使用的其他一些數據類型。<br>
<br>