<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 顯式接口實現(C# 編程指南) 如果[類](https://msdn.microsoft.com/zh-CN/library/0b0thckt.aspx)實現兩個接口,并且這兩個接口包含具有相同簽名的成員,那么在類中實現該成員將導致兩個接口都使用該成員作為它們的實現。在下面的示例中,所有對 Paint 調用方法相同。 ``` class Test { static void Main() { SampleClass sc = new SampleClass(); IControl ctrl = (IControl)sc; ISurface srfc = (ISurface)sc; // The following lines all call the same method. sc.Paint(); ctrl.Paint(); srfc.Paint(); } } interface IControl { void Paint(); } interface ISurface { void Paint(); } class SampleClass : IControl, ISurface { // Both ISurface.Paint and IControl.Paint call this method. public void Paint() { Console.WriteLine("Paint method in SampleClass"); } } // Output: // Paint method in SampleClass // Paint method in SampleClass // Paint method in SampleClass ``` 然而,如果兩個[接口](https://msdn.microsoft.com/zh-CN/library/87d83y5b.aspx)成員執行不同的函數,那么這可能會導致其中一個接口的實現不正確或兩個接口的實現都不正確。可以顯式地實現接口成員 -- 即創建一個僅通過該接口調用并且特定于該接口的類成員。這是使用接口名稱和一個句點命名該類成員來實現的。例如: ``` public class SampleClass : IControl, ISurface { void IControl.Paint() { System.Console.WriteLine("IControl.Paint"); } void ISurface.Paint() { System.Console.WriteLine("ISurface.Paint"); } } ``` 類成員 **IControl.Paint** 只能通過 **IControl** 接口使用,**ISurface.Paint** 只能通過 **ISurface** 使用。兩個方法實現都是分離的,都不可以直接在類中使用。例如: ``` // Call the Paint methods from Main. SampleClass obj = new SampleClass(); //obj.Paint(); // Compiler error. IControl c = (IControl)obj; c.Paint(); // Calls IControl.Paint on SampleClass. ISurface s = (ISurface)obj; s.Paint(); // Calls ISurface.Paint on SampleClass. // Output: // IControl.Paint // ISurface.Paint ``` 顯式實現還用于解決兩個接口分別聲明具有相同名稱的不同成員(如屬性和方法)的情況: ``` interface ILeft { int P { get;} } interface IRight { int P(); } ``` 為了同時實現兩個接口,類必須對屬性 P 和/或方法 P 使用顯式實現以避免編譯器錯誤。例如: ``` class Middle : ILeft, IRight { public int P() { return 0; } int ILeft.P { get { return 0; } } } ``` ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-CN/library/67ef8sbd.aspx) [類和結構(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ms173109.aspx) [接口(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ms173156.aspx) [繼承(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ms173149.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看