C# 介面的定義和使用

來源:互聯網
上載者:User

在進行介面映射時,還要注意下面兩點:

  1、在決定由類中的哪個成員來實現介面成員時,類中顯式說明的介面成員比其它成員優先實現。

  2、使用Private、protected和static修飾符的成員不能參與實現介面映射。例如:

 

 

代碼

interface ICloneable { 
 object Clone( ) ;
}
class C: ICloneable {
 object ICloneable.Clone( ) {…}
 public object Clone( ) {…}
}

  /*例子中成員ICloneable.Clone 稱為介面ICloneable 的成員Clone 的實現者,因為它是顯式說明的介面成員,比其它成員有著更高的優先權。

  如果一個類實現了兩個或兩個以上名字、類型和參數類型都相同的介面,那麼類中的一個成員就可能實現所有這些介面成員: */

interface IControl {
 void Paint( ) ;
}
interface IForm {
 void Paint( ) ;
}
class Page: IControl, IForm {
 public void Paint( ) {…}
}
  //這裡,介面IControl和IForm的方法Paint都映射到了類Page中的Paint方法。當然也可以分別用顯式的介面成員分別實現這兩個方法:
interface IControl {
 void Paint( ) ;
}
interface IForm {
 void Paint( ) ;
}
class Page: IControl, IForm {
 public void IControl.Paint( ) {
 //具體的介面實現代碼
}
public void IForm.Paint( ) {
 //具體的介面實現代碼
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.