接口可以理解為是一種標(biāo)準(zhǔn),在這種標(biāo)準(zhǔn)中規(guī)定了實(shí)現(xiàn)其的類(lèi)及結(jié)構(gòu)體中至少應(yīng)包含的方法和屬性。在C#語(yǔ)言中不允許類(lèi)的多繼承,這是因?yàn)閭鹘y(tǒng)的多繼承帶來(lái)的問(wèn)題往往勝過(guò)其帶來(lái)的好處。然而,現(xiàn)實(shí)世界里到處都存在著多繼承的情況。為了解決這個(gè)矛盾,在一些面向?qū)ο笳Z(yǔ)言中提出了接口的概念。在C#中,通過(guò)接口可以實(shí)現(xiàn)多繼承的功能。
1.接口的聲明
訪(fǎng)問(wèn)修飾符 interface 接口名稱(chēng){:基接口}
{
接口體;
}
如下面定義了一個(gè)控件的接口IControl:
interface IControl
{
void Paint();
}
與類(lèi)不同,接口如果沒(méi)有指定訪(fǎng)問(wèn)修飾符,其默認(rèn)為public。接口的成員只能為方法,屬性,索引器和事件。
接口中的任何成員僅有聲明,沒(méi)有實(shí)現(xiàn),而且也不能實(shí)現(xiàn),因?yàn)榻涌趦H是一種契約,這種契約需要類(lèi)或結(jié)構(gòu)來(lái)實(shí)現(xiàn)。
接口中的任何成員都是定義為公有的,指定其它的訪(fǎng)問(wèn)修飾符,編譯時(shí)將會(huì)出錯(cuò)。
2.接口的繼承
接口可以象類(lèi)那樣進(jìn)行繼承,但與類(lèi)不同的是,接口具有多重繼承性,而類(lèi)沒(méi)有。下面是兩個(gè)單繼承的例子:
interface ITextBox:IControl
{
void SetText(string txt);
}
interface IListBox:IControl
{
void SetItems(string[] items)
}
而下面是接口多重繼承的例子:
interface IComboBox:TextBox,ListBox
{
}
從上面的例子中可以看出,如果接口具有多個(gè)基接口,每個(gè)基接口之間用逗號(hào)隔開(kāi)。
3.接口的實(shí)現(xiàn)
因?yàn)榻涌谥械某蓡T僅有聲明而沒(méi)有實(shí)現(xiàn),這樣的話(huà),接口本身是什么都干不了的,必須由其它內(nèi)容來(lái)實(shí)現(xiàn)其中的聲明才會(huì)起作用。
類(lèi)和結(jié)構(gòu)都可以實(shí)現(xiàn)接口。這里僅探討類(lèi)實(shí)現(xiàn)接口的情況。一個(gè)類(lèi)可以實(shí)現(xiàn)一個(gè)接口,也可以實(shí)現(xiàn)多個(gè)接口,類(lèi)要想實(shí)現(xiàn)接口,必須實(shí)現(xiàn)接口中聲明的全部成員,否則無(wú)法通過(guò)編譯。下面給出接口使用的例子:
using System;
interface Vehicle //交通工具接口
{
string Color //顏色
{
get;
set;
}
void SpeedUp(float v); //加速運(yùn)行
void Stop(); //停止
}
interface Toy //玩具接口
{
void Cry(); //哭
void Laugh(); //笑
}
interface Car:Vehicle //小汽車(chē)接口,繼承于Vehicle接口
{
int Container //汽車(chē)的容量
{
get;
set;
}
}
interface Ship:Vehicle //輪船接口,繼承于Vehicle接口
{
string Type //型號(hào)
{
get;
}
}
class Titanic:Ship //定義泰坦尼克類(lèi)Titanic實(shí)現(xiàn)Ship接口
{
string s_color; //顏色
string s_type; //型號(hào)
public Titanic(string s) //構(gòu)造器
{
this.s_type = s;
}
public string Type
{
get{return s_type;}
}
public string Color //實(shí)現(xiàn)顏色Color屬性
{
get{return s_color;}
set{s_color = value;}
}
public void SpeedUp(float v) //實(shí)現(xiàn)加速運(yùn)行方法
{
Console.WriteLine("泰坦尼克以{0}m/s的速度全速前進(jìn)",v);
}
public void Stop() //實(shí)現(xiàn)停止方法
{
Console.WriteLine("泰坦尼克停下來(lái)休息!");
}
}
class ToyCar:Toy,Car //定義玩具汽車(chē)類(lèi)ToyCar實(shí)現(xiàn)Toy和Car接口
{
string s_color; //顏色
int i_container; //容量
public string Color //實(shí)現(xiàn)顏色Color屬性
{
get{return s_color;}
set{s_color = value;}
}
public int Container //實(shí)現(xiàn)容量屬性
{
get{return i_container;}
set{i_container = value;}
}
public void SpeedUp(float v) //實(shí)現(xiàn)加速運(yùn)行方法
{
Console.WriteLine("玩具汽車(chē)以{0}m/s的速度全速前進(jìn)",v);
}
public void Stop() //實(shí)現(xiàn)停止方法
{
Console.WriteLine("玩具汽車(chē)??吭谂赃?!");
}
public void Cry() //實(shí)現(xiàn)哭Cry()方法
{
Console.WriteLine("玩具汽車(chē)哭了...(!_!)");
}
public void Laugh() //實(shí)現(xiàn)笑Laugh()方法
{
Console.WriteLine("玩具汽車(chē)笑了...(^_^)");
}
//類(lèi)的其它方法
public void PrintInfo()
{
Console.Write("玩具汽車(chē)的顏色為:{0},",s_color);
Console.WriteLine("容量為{0}",i_container);
}
}
新聞熱點(diǎn)
疑難解答
圖片精選