接口
13.1 什麼是接口
接口是指定一組函數成員而不實現它們的引用類型,所以只能類和結構來實現接口
13.2 聲明接口:
接口的聲明不能包含以下成員:
- 數據成員
- 靜態成員
接口的聲明只能包含如下類型的非靜態成員函數的聲明:
- 方法
- 屬性
- 事件
- 索引器
注意:
- 這些函數的聲明不能包含任何實現代碼,而在每一個成員聲明的主體後必須使用分號
- 按照慣例,接口名必須從大寫的 I 開始(如:ISaveable)
- 與類和結構一樣,接口聲明還可以分隔成分部接口聲明
eg:
// 關鍵字 接口名
interface IMyInterface1
{
int DoStuff(int nVar1, long lVar2);
double DoOtherStuff(string s, long x);
}
- 接口聲明可以有任何訪問的修飾符
public 、protected 、internal 或 private
- 接口的成員是隱式 public 的,不允許有任何訪問修飾符,包括 public
13.3 實現接口
ps:實現 IMyInterface1 接口
// 關鍵字 接口名
class MyClass:IMyInterface1
{
int DoStuff(int nVar1, long lVar2)
{
...
//實現代碼
}
double DoOtherStuff(string s, long x)
{
...
//實現代碼
}
}
下面實現一個簡單的接口
//聲明接口
interface IIfc1
{
void PrintOut(string s);
}
// 實現接口
class MyClass:IIfc1 // 聲明類
{
// 實現
public void PrintOut(string s)
{
Console.Mriteline("Calling through:{0)",s);
}
}
class Program
{
static void Main()
{
// 創建實例
MyClass mc = new MyClass();
// 調用方法
mc.PrintOut("object");
}
}
// 輸出: Calling through:object
13.4 接口是引用類型
接口不僅僅是類或結構要實現的成員列表。它是一個引用類型
13.5 實現多個接口
// 聲明接口
interface IDataRetrieve
{
int GetData();
}
interface IDataStore
{
void SetData( int x );
}
// 聲明類
// 接口 接口
class MyData:IDataRetrieve,IDataStore
{
int Mem1; // 聲明字段
public int GetData() {
return Mem1;
}
public void SetData( int x)
{
Mem1=x;
}
}
class Program
{
static void Main()
{
MyData data = new MyData();
data.SetData( 5);
Console.MriteLine("Value=(0)",data.GetData());
}
}
// Value = 5
13.6 實現具有重複成員的接口
// 聲明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
interface IDataStore
{
void PrintOut(string t);
}
//實現兩個接口
class MyClass:IIfc1,IIfc2
{
public void PtintOut(string s) // 兩個接口的單一實現
{
Console.WriteLine("Calling through:{0}",s);
}
}
class Program
{
static void Main()
{
MyClass mc = new MyClass();
mc.PrintOut("Object");
}
}
// 輸出: Calling through:Object
13.7 多個接口的引用
// 聲明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
interface IDataStore
{
void PrintOut(string t);
}
//實現兩個接口
class MyClass:IIfc1,IIfc2
{
public void PtintOut(string s) // 兩個接口的單一實現
{
Console.WriteLine("Calling through:{0}",s);
}
}
class Program
{
static void Main()
{
MyClass mc = new MyClass();
IIfc1 ifc1 = (IIfc1) mc; // 獲取IIfc1的引用
IIfc2 ifc2 = (IIfc2) mc;// 獲取IIfc2的引用
mc.PrintOut("Object"); // 從類調用對象
ifc1.PrintOut("interface 1"); // 從IIfc1調用
ifc2.PrintOut("interface 2"); // 從IIfc2調用
}
}
13.8 派生成員作爲實現
// 聲明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
//實現兩個接口
class MyBaseClass
{
public void PtintOut(string s) // 實現方法
{
Console.WriteLine("Calling through:{0}",s);
}
}
//聲明類
class Derived:MyClass,IIfc1 {}
class Program
{
static void Main()
{
Derived d = new Derived();// 創建類對象
d.PtintOut("object"); // 調用方法
}
}
13.9 顯式接口成員實現
顯式接口成員實現有如下特性:
- 與所有接口實現相似,位於實現了接口的類或結構中
- 它使用限定接口名稱來聲明,由接口名稱和成員名稱以及它們中間的點分隔符號構成
顯式接口成員實現:
class MyClass:IIfc1,IIfc2
{
void IIfc1.PrintOut(string s)
{
...
}
void IIfc2.PrintOut(string s)
{
...
}
}
13.10 接口可以繼承接口
接口實現可以從基類被繼承,而接口本身也可以從一個或多個接口繼承
- 要指定某個接口繼承其他的接口,應在接口聲明中把基接口以逗號分隔的列表形式放在接口名稱的冒號後面
// 接口名 基接口列表
interface IDataIO : IDataRetrieve, IDataStore
{
...
}
- 與類不同,它在基類列表中只能有一個類名,而接口可以在基接口列表中有任意多個
接口- ■ 列表中的接口本身可以繼承其他接口
- ■ 結果接口包含它聲明的所有接口和所有基接口的成員
interface IDataRetrieve
{
int GetData();
}
interface IDataStore
{
void SetData( intx);
}
// 從前兩個接口繼承
interface IDataIO:IDataRetrieve,IDataStore{}
class MyData:IDataIO
{
int nPrivateData;
public int GetData()
{
return nPrivateData;
}
public void SetData(int x)
{
nPrivateData = x;
}
}
class Program{
static void Main()
{
MyData data • new MyData();
data.SetData(5);
Console.MriteLine("(0)", data.GetData());
}
}
13.11 不同類實現同一個接口的示例
interface ILiveBirth // 聲明接口
{
string BabyCalled();
}
class Animal{} // 基類Animal
class Cat : Animal,ILiveBirth //聲明Cat類
{
string ILiveBirth.BabyCalled()
{
return"kitten";
}
}
class Dog : Animal,ILiveBirth // 聲明Dog類
{
string ILiveBirth.BabyCalled()
{
return"puppy";
}
}
class Bird : Animal{} //聲明Bird類
class Program
{
static void Main()
{
Animal[] animalArray = new Animal[3]; // 創建Animal數姐
animalArray[o]= new Cat(); // 插入Cat類對象
animalArray[1] = new Bird(); // 插入Bird奏對象
animalArray[2] = new Dog(); // 插入Dog奏對象
foreach( Animal a in animalArray ) // 在數組中循環
{
ILiveBirth b = a as ILiveBirth; //如果實現ILiveBirth...
if (b != null)
Console.Mriteline("Baby is called:(0)", b.BabyCalled());
}
}
}