一 什么是介面
介面是指定一組函式成員而不實作它們的參考型別,
class Program { static void FlyFunc(IFly obj) { obj.Fly(); } static void Main(string[] args) { var bird = new Bird(); var butterfly = new Butterfly(); FlyFunc(bird); FlyFunc(butterfly); Console.Read(); } } //宣告一個IFly介面 interface IFly { void Fly(); } class Bird : IFly { public void Fly() { Console.WriteLine("Bird Fly"); } } class Butterfly : IFly { public void Fly() { Console.WriteLine("Butterfly Fly"); } }
二 使用IComparable介面的示例
Array類有一個靜態方法Sort(),可以排序元素,
var array = new int[] { 50, 15, 59, 88, 14 }; Array.Sort(array); foreach (var item in array) Console.Write($"{item} "); //輸出 14 15 50 59 88
Array類的Sort方法倚賴于IComparable介面,它宣告在BCL中,包含唯一的方法CompareTo,
.NET檔案中描述了CompareTo方法的作用,在呼叫CompareTo方法時,它應該回傳以下幾個值:
- 負數值:當前物件小于引數物件;
- 正數值:當前物件大于引數物件;
- 零:兩個物件在比較時相等;
Sort使用的演算法倚賴于元素的CompareTo方法來決定兩個元素的次序,
class Program { static void Main(string[] args) { var myInt = new int[] { 10, 20, 1, 5, 50 }; var mcArr = new MyClass[5]; for (int i = 0; i < 5; i++) { mcArr[i] = new MyClass() { TheValue =https://www.cnblogs.com/wwwen/archive/2022/04/28/ myInt[i] }; } foreach (var item in mcArr) { Console.Write($"{item.TheValue} "); } Console.WriteLine(); Array.Sort(mcArr); //排序 foreach (var item in mcArr) { Console.Write($"{item.TheValue} "); } Console.ReadLine(); //輸出: 10 20 1 5 50 1 5 10 20 50 } } //自定義的MyClass類,實作IComparable介面 class MyClass : IComparable { public int TheValue { get; set; } public int CompareTo(object obj) { var mc = (MyClass)obj; if (this.TheValue < mc.TheValue) return -1; if (this.TheValue > mc.TheValue) return 1; return 0; } }
三 宣告介面
介面宣告不能包含資料成員和靜態成員,
介面宣告只能包含以下型別的非靜態成員函式的宣告:
- 方法
- 屬性
- 事件
- 索引器
這些函式成員的宣告不能包含任何實作代碼,而在每一個成員宣告的主體后必須使用分號,
按照慣例,介面名稱以大寫 I 開頭,比如 ISaveable,
與類和結構一樣,介面宣告還可以分隔成分部介面宣告,
介面宣告可以有任何的訪問修飾符,public、protected、internal、private,
介面的成員是隱式public的,不允許有任何修飾符,包括public,
四 實作介面
只有類和結構才能實作介面,
要實作介面,類和結構必須:
- 在基類串列中包括介面名稱
- 為每一個介面成員提供實作
//宣告介面
interface IMyInterface { int ID { get; set; } void Method(string s); } //實作介面 class MyInterface : IMyInterface { public int ID { get; set; } public void Method(string s) { Console.WriteLine(s); } }
類和結構可以實作任意數量的介面,
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; } } static void Main(string[] args) { var myData = https://www.cnblogs.com/wwwen/archive/2022/04/28/new MyData(); myData.SetData(5); Console.WriteLine($"data is : {myData.GetData()}"); }
五 實作具有重復成員的介面
由于類可以實作任意數量的介面,有可能兩個或多個介面成員都有相同的簽名和回傳型別,
這種情況下,類可以實作單個成員滿足所有包含重復成員的介面,
interface IIfc1 { void PrintOut(string s); } interface IIfc2 { void PrintOut(string s); } class MyClass : IIfc1, IIfc2 //實作兩個介面 { void PrintOut(string s) //兩個介面的單一實作 { Console.WriteLine(s); } }
六 顯示介面成員實作
如果希望為每一個介面分離實作,可以通過創建顯示介面成員實作,
class MyClass : IIfc1, IIfc2 //實作兩個介面 { void IIfc1.PrintOut(string s) //顯示實作IIfc1 { Console.WriteLine($"IIfc1 : {s}"); } void IIfc2.PrintOut(string s) //顯示實作IIfc2 { Console.WriteLine($"IIfc2 : {s}"); } }
七 派生成員作為實作
實作介面的類可以從它的基類繼承實作的代碼
interface IIfc1 { void PrintOut(string s); } interface IIfc2 { void PrintOut(string s); } class MyClass : IIfc1, IIfc2 //實作兩個介面 { void IIfc1.PrintOut(string s) //顯示實作IIfc1 { Console.WriteLine($"IIfc1 : {s}"); } void IIfc2.PrintOut(string s) //顯示實作IIfc2 { Console.WriteLine($"IIfc2 : {s}"); } } class MyDerivedClass : MyClass { } static void Main(string[] args) { var myDericed = new MyDerivedClass(); ((IIfc1)myDericed).PrintOut("111"); ((IIfc2)myDericed).PrintOut("222"); Console.Read(); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/467849.html
標籤:.NET技术
