<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • C#接口方法的實(shí)例介紹

            本篇內(nèi)容介紹了“C#接口方法的實(shí)例介紹”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

            創(chuàng)新互聯(lián)于2013年開(kāi)始,先為新晃等服務(wù)建站,新晃等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為新晃企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

            1.公有方法實(shí)現(xiàn)C#接口方法

            盡管C#在定義接口時(shí)不用指明接口方法的訪問(wèn)控制方式,但默認(rèn)接口方法均為public型(這可以從反編譯的IL代碼中看到)。下面是使用Reflector查看的接口IL代碼:

            class private interface abstract auto ansi IControl{  method public hidebysig newslot abstract virtual instance void Paint() cil managed{  }  }

            實(shí)現(xiàn)接口的類(lèi)需要實(shí)現(xiàn)所有接口方法。通常情況下,接口的實(shí)現(xiàn)方法也為public型。如下案例:

            using System ;  interface IControl  {  void Paint();  }  public class EditBox: IControl  {  public void Paint()  {  Console.WriteLine("Pain method is called!");  }  }  class Test  {  static void Main()  {  EditBox editbox = new EditBox();  editbox.Paint();  ((IControl)editbox)。Paint();  }  }

            接口就好像是關(guān)系型數(shù)據(jù)庫(kù)中的一對(duì)多表,一個(gè)接口對(duì)應(yīng)多個(gè)接口方法,每個(gè)接口方法又對(duì)應(yīng)虛擬方法表(VMT)中的某個(gè)公有或私有方法??梢?jiàn)通過(guò)接口對(duì)方法進(jìn)行調(diào)用需要多出一道轉(zhuǎn)換工作,因此執(zhí)行效率不如直接調(diào)用。

            2.私有方法不能實(shí)現(xiàn)C#接口方法

            如果想將接口方法直接實(shí)現(xiàn)為私有方法是辦不到的。下面的EditBox的代碼中Paint方法沒(méi)有特殊說(shuō)明,默認(rèn)為private,導(dǎo)致代碼無(wú)法執(zhí)行:

            using System ;  interface IControl  {  void Paint();  }  public class EditBox: IControl  {  void Paint()  {  Console.WriteLine("Pain method is called!");  }  public void ShowPaint()  {  this.Paint();  ((IControl)this)。Paint();  }  }  class Test  {  static void Main()  {  EditBox editbox = new EditBox();  editbox.ShowPaint();  }  }

            程序在編譯時(shí)將顯示如下編譯錯(cuò)誤:“”EditBox“不會(huì)實(shí)現(xiàn)接口成員”IControl.Paint()“?!盓ditBox.Paint()“或者是靜態(tài)、非公共的,或者有錯(cuò)誤的返回類(lèi)型?!?/p>

            由于接口規(guī)范中的方法默認(rèn)的訪問(wèn)權(quán)限是public,而類(lèi)中的默認(rèn)訪問(wèn)權(quán)限是default,也就是說(shuō)private,因此導(dǎo)致權(quán)限范圍收縮,兩者權(quán)限并不相同,所以必須將類(lèi)的權(quán)限調(diào)整為public才可以使上面的代碼得以執(zhí)行。

            3.實(shí)現(xiàn)專(zhuān)門(mén)的C#接口方法

            using System ;  interface IControl  {  void Paint();  }  public class EditBox: IControl  {  void Paint()  {  Console.WriteLine("Pain method is called!");  }  void IControl.Paint()  {  Console.WriteLine("IControl.Pain method is called!");  }  public void ShowPaint()  {  this.Paint();  ((IControl)this)。Paint();  }  }  class Test  {  static void Main()  {  EditBox editbox = new EditBox();  editbox.ShowPaint();  //editbox.Paint();  ((IControl)editbox)。Paint();  }  }

            “C#接口方法的實(shí)例介紹”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

            網(wǎng)頁(yè)題目:C#接口方法的實(shí)例介紹
            文章網(wǎng)址:http://www.jbt999.com/article26/jsdgjg.html

            成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)品牌網(wǎng)站制作、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站維護(hù)、ChatGPTApp開(kāi)發(fā)

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

            成都app開(kāi)發(fā)公司

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 午夜精品久久久久久久免费APP | 久久久九九| 蜜桃无码AV | 操逼网站首页 | 园产夫妻操逼免费操逼电影 |