国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#教程第六課:C#的名稱空間

2024-07-21 02:22:17
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 本節課將介紹c#的名稱空間。其目的是: <br>
    1.了解什么是名稱空間。 <br>
    <br>
    2.了解如何實現&quot;using&quot;指示符。 <br>
    <br>
    3.了解&quot;alias&quot; 指示符的用法。 <br>
    <br>
    4.了解名稱空間的成員的內容。 <br>
    <br>
    在第一課中,你已經在簡單的hello程序中看到了&quot;using system;&quot;指示符的使用。該指示符可以讓你使用system名稱空間中的成員。在第一課中,未及對此作出詳細介紹,現在我們來解釋一下名稱空間的具體用法。一旦學完了本節課,你將了解&quot;using&quot;指示符及其相關內容。 <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(&quot;this is the new c# station namespace.&quot;); <br>
    }<br>
    }<br>
    } <br>
    <br>
    說明 <br>
    <br>
    清單6-1演示了如何創建一個名稱空間。把單詞&quot;namespace&quot;放在&quot;csharp_station&quot;之前,就創建了一個名稱空間。&quot;csharp_station&quot;名稱空間內的大括號中包含了成員。 <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(&quot;this is the new c# <br>
    station tutorial namespace.&quot;); <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(&quot;this is the new c# station tutorial namespace.&quot;); <br>
    }<br>
    }<br>
    } <br>
    <br>
    說明 <br>
    <br>
    清單6-3演示了另外一種編寫嵌套的名稱空間的方法。在&quot;csharp_station&quot;和&quot;tutorial&quot;之間置入點運算符,表明這是嵌套的名稱空間。結果同清單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(&quot;first example of calling another namespace member.&quot;);<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(&quot;second example of calling another namespace member.&quot;);<br>
    }<br>
    }<br>
    } <br>
    <br>
    說明 <br>
    <br>
    1.清單6-4 的例子演示了用完整的名稱指示,調用名稱空間的成員。 <br>
    <br>
    一個完整的名稱指示包括名稱空間名,以及調用的方法名。程序的上半部分,在&quot;csharp-station&quot;名稱空間內嵌套的名稱空間&quot;tutorial&quot;中,定義了類&quot;myexample1&quot;及其方法&quot;myprint1&quot;。 main()方法中用完整的名稱指示:&quot;tutorial.myexample1.myprint()&quot; 來進行調用。 因為main()方法和tutorial名稱空間位于同一名稱空間內,如果使用&quot;csharp_station&quot;的全稱不是必需的。 <br>
    <br>
    2.清單6-4的下半部分,也是名稱空間&quot;csharp_station.tutorial&quot;的一部分。 <br>
    <br>
    類&quot;myexample1&quot;和&quot;myexample2&quot;都屬于該名稱空間。另外,也可以把它們分別寫入不同的文件,同時它們仍然屬于同一名稱空間。在main()方法中,調用&quot;myprint2&quot;方法時,采用了全稱:&quot;csharp_station.tutorial.myexample2.myprint2()&quot;。 在這里,必須使用全稱中&quot;csharp_station&quot;,因為&quot;myexample2&quot;定義在外部。 <br>
    <br>
    3.注意:這里對兩個不同的類起了不同的名字: <br>
    <br>
    &quot;myexample1&quot;和&quot;myexample2&quot;這是因為對于每個名稱空間來說,其中的成員必須有唯一的名稱。 記住:它們都處于同一名稱空間中,不能取名相同。方法&quot;myprint1&quot;和&quot;myprint2&quot; 名稱的不同僅僅是為了方便起見,即使同名也沒有問題,因為它們屬于不同的類。 <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(&quot;example of using a using directive.&quot;);<br>
    }<br>
    } <br>
    <br>
    說明 <br>
    <br>
    調用方法時,如果你不想打入全稱,可使用&quot;using&quot;指示符。在清單6-5中,有兩個&quot;using&quot;指示符。第一個指示符是&quot;using system&quot;,同本教程其它地方出現的&quot;using&quot;指示符相同。你不需要每次都打上&quot;system&quot;,只需要打入該名稱空間的成員方法名即可。在myprint()中,&quot;console&quot;是個&quot;system&quot;名稱空間中的成員類,該類有個&quot;writeline&quot;的方法。該方法的全稱是: &quot;system.console.writeline(...)&quot;。 <br>
    <br>
    類似地,using指示符&quot;using csharp_station.tutorial&quot;可以讓我們在使用 &quot;csharp_station.tutorial&quot; 名稱空間的成員時,無需打入全稱。所以,我們可以打入&quot;myexample.myprint()&quot;。如果不使用&quot;using&quot;指示符,每次實現該方法時,我們就得打入&quot;csharp_station.tutorial.myexample.myprint()&quot; 。 <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(&quot;not a member of <br>
    csharp_station.tutorial.myexample.&quot;);<br>
    }<br>
    }<br>
    <br>
    // c# station tutorial namespace<br>
    namespace csharp_station.tutorial {<br>
    class myexample {<br>
    public static void myprint() {<br>
    console.writeline(&quot;this is a member of csharp_station.tutorial.myexample.&quot;);<br>
    }<br>
    }<br>
    } <br>
    <br>
    說明 <br>
    <br>
    1.有時,往往遇到取名較長的名稱空間,而你可以把該名稱變短些。 <br>
    <br>
    這樣就增強了可讀性,還避免了同名的沖突。清單6-6 演示了如何使用別名指示符,創建別名的格式例子是:&quot;using cstut = csharp_station.tutorial.myexample&quot;。表達式&quot;cstut&quot;可以取代&quot;csharp_station.tutorial.myexample&quot;,用在本文件的任何地方。在main()方法中就使用了&quot;cstut&quot;。 <br>
    <br>
    2.在main()方法中,調用了&quot;aliasdirective&quot; 類中&quot;myprint&quot; 方法。 <br>
    <br>
    這與&quot;myexample&quot; 類的&quot;myprint&quot;方法同名。 雖然同名,這兩個方法都各自正確地進行了調用,原因是:&quot;myexample&quot;類的&quot;myprint&quot;方法用別名&quot;cstut&quot;表示。編譯器能夠準確地了解所要執行的是哪個方法。一旦漏掉了&quot;cstut&quot;,編譯器將兩次調用&quot;aliasdirective&quot;類的&quot;myprint&quot;方法。 <br>
    <br>
    3.另外一方面,如果我們沒有創建別名指示符,而是添加了&quot;using csharp_station.tutorial.myexample&quot;之后,再調用myprint(),編譯器就會生成出錯信息,因為它不知道究竟是調用. &quot;csharp_station.tutorial.myexample.myprint()&quot;方法呢?還是去調用&quot;aliasdirective.myprint()&quot;方法。所以使用名稱空間是良好的編程習慣,可避免代碼中的沖突現象。 <br>
    <br>
    小結 <br>
    到現在為止,我們已經了解在名稱空間中可以使用類,實際上,名稱空間可以使用如下類型的數據: <br>
    <br>
    類;結構;接口;枚舉;代理 <br>
    <br>
    在后面的課程中我們將詳細介紹這些數據類型。 <br>
    <br>
    概括來講,你已經了解了什么是名稱空間,如何定義自己的名稱空間。如果你不想打入全稱,可以使用&quot;using&quot;指示符。一旦你想縮短名稱空間的長名,可以使用別名指示符。另外,除了類之外,你也了解了名稱空間可以使用的其他一些數據類型。<br>
    <br>
    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 临桂县| 庆城县| 阳东县| 曲靖市| 禹州市| 太原市| 满洲里市| 台中市| 镇坪县| 五家渠市| 茌平县| 米泉市| 鹤岗市| 海安县| 呼玛县| 蒙阴县| 合肥市| 三明市| 东光县| 丹凤县| 唐河县| 河津市| 聂荣县| 依兰县| 赤水市| 黑龙江省| 安乡县| 阳朔县| 文水县| 女性| 阿拉善右旗| 鸡泽县| 韶关市| 丽江市| 固阳县| 泸州市| 浦北县| 原平市| 杭州市| 济阳县| 普安县|