自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#類實(shí)現(xiàn)接口簡(jiǎn)單介紹

開發(fā) 后端
這里介紹C#類實(shí)現(xiàn)接口的例子,其中ISequence 為一個(gè)隊(duì)列接口,提供了向隊(duì)列尾部添加對(duì)象的成員方法Add( ),IRing 為一個(gè)循環(huán)表接口。

本文向大家介紹C#類實(shí)現(xiàn)接口,可能好多人還不知道C#類實(shí)現(xiàn)接口,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

C#類實(shí)現(xiàn)接口

前面我們已經(jīng)說過,接口定義不包括方法的實(shí)現(xiàn)部分。接口可以通過類或結(jié)構(gòu)來實(shí)現(xiàn)。我們主要講述通過類來實(shí)現(xiàn)接口。用類來實(shí)現(xiàn)接口時(shí),接口的名稱必須包含在類定義中的基類列表中。

下面的例子給出了C#類實(shí)現(xiàn)接口的例子。其中ISequence 為一個(gè)隊(duì)列接口,提供了向隊(duì)列尾部添加對(duì)象的成員方法Add( ),IRing 為一個(gè)循環(huán)表接口,提供了向環(huán)中插入對(duì)象的方法Insert(object obj),方法返回插入的位置。類RingSquence 實(shí)現(xiàn)了接口ISequence 和接口IRing。

  1. using System ;  
  2. interface ISequence {  
  3. object Add( ) ;  
  4. }  
  5. interface ISequence {  
  6. object Add( ) ;  
  7. }  
  8. interface IRing {  
  9. int Insert(object obj) ;  
  10. }  
  11. class RingSequence: ISequence, IRing  
  12. {  
  13. public object Add( ) {…}  
  14. public int Insert(object obj) {…}  

如果類實(shí)現(xiàn)了某個(gè)接口,類也隱式地繼承了該接口的所有父接口,不管這些父接口有沒有在類定義的基類表中列出??聪旅娴睦樱?/P>

  1. using System ;  
  2. interface IControl {  
  3. void Paint( );  
  4. }  
  5. interface ITextBox: IControl {  
  6. void SetText(string text);  
  7. }  
  8. interface IListBox: IControl {  
  9. void SetItems(string[] items);  
  10. }  
  11. interface IComboBox: ITextBox, IListBox { } 

這里, 接口IcomboBox繼承了ItextBox和IlistBox。類TextBox不僅實(shí)現(xiàn)了接口ITextBox,還實(shí)現(xiàn)了接口ITextBox 的父接口IControl。

前面我們已經(jīng)看到,一個(gè)類可以實(shí)現(xiàn)多個(gè)接口。再看下面的例子:

  1. interface IDataBound {  
  2. void Bind(Binder b);  
  3. }  
  4. public class EditBox: Control, IControl, IDataBound {  
  5. public void Paint( );  
  6. public void Bind(Binder b) {...}  

類EditBox從類Control中派生并且實(shí)現(xiàn)了Icontrol和IdataBound。在前面的例子中接口Icontrol中的Paint方法和IdataBound接口中的Bind方法都用類EditBox中的公共成員實(shí)現(xiàn)。C#提供一種實(shí)現(xiàn)這些方法的可選擇的途徑,這樣可以使執(zhí)行這些的類避免把這些成員設(shè)定為公共的。C#類實(shí)現(xiàn)接口成員可以用有效的名稱。

【編輯推薦】

  1. C# this關(guān)鍵字詳解
  2. C#調(diào)用析構(gòu)方法詳解
  3. C#釋放托管資源簡(jiǎn)單描述
  4. C#單路代理簡(jiǎn)單分析
  5. C#文法產(chǎn)生式概述
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-08-06 14:53:41

C# User類

2009-08-27 13:37:11

C#類和結(jié)構(gòu)

2009-09-02 18:03:19

C#實(shí)現(xiàn)泛型類

2009-08-12 09:41:28

C# Director

2009-09-03 15:57:11

C# SystemMe

2009-08-07 17:41:40

C#預(yù)處理

2009-08-27 10:19:22

C#匿名類型

2009-08-10 16:19:37

C#冒泡排序

2009-08-14 17:27:56

C#方法參數(shù)

2009-08-18 17:37:57

C#固定指針

2009-08-21 17:55:52

C#復(fù)合控件

2009-09-01 16:19:57

C# new()約束

2009-08-03 17:51:43

C#引用類型

2009-08-06 18:15:13

C# SQL Serv

2009-08-07 17:12:07

C# DLL函數(shù)

2009-08-13 17:36:54

編譯C#代碼

2009-08-14 16:46:44

C#元數(shù)據(jù)

2009-08-20 16:25:59

C# 匿名方法

2009-08-25 13:38:35

C# Timer組件

2009-09-03 09:40:57

C#創(chuàng)建表單
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)