標籤:style blog http color ar os 使用 sp for
類實現一個介面時,它必須實現該介面的所有部分(方法和屬性等),效果相當於類要表明:“我同意履行這個介面所定義的協定。”
從抽象類別繼承實現了“is-a(是一種)”關係,實現介面時一種“implement(實現)”關係,區別在於:
舉個例子:汽車是一種運載工具,它可以實現CanBeBoughtWithABigLoan(可貸巨款購買)這種能力(就像房子一樣)
1 /************************************************************************************* 2 * 3 * 1.如果定義一個Document類,這個類可以儲存,又可以壓縮,所以要同時實現IStorable和ICompressible介面 4 * 2.擴充介面就是用一個新街口擴充原來的介面,通過擴充介面我們表示了這樣的意思: 5 * 實現了新介面的任何東西也必須實現原來的介面 6 * 3.組合介面就是將已有的介面組合起來,並且可以增加新的方法或者屬性來建立新的介面 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 *************************************************************************************/ 17 using System; 18 19 namespace SimpleInterface 20 { 21 //第一個介面 22 interface IStorable 23 { 24 //介面的方法聲明中沒有存取修飾詞,隱含是public的,因為介面是要有其他類使用的協定 25 void Read(); 26 void Write(Object obj); 27 //屬性的聲明並未實現get和set方法,只是聲明這兩個方法 28 int Status { get; set; } 29 } 30 31 //第二個介面 32 interface ICompressible 33 { 34 void Compress(); 35 void Decompress(); 36 } 37 38 //擴充介面 39 interface ILoggedCompressible : ICompressible 40 { 41 //新介面增加了一個新的方法記錄節省的位元組數 42 void LogSavedBytes(); 43 } 44 45 //組合介面 46 interface IStorableCompressible : IStorable, ILoggedCompressible 47 { 48 //儲存壓縮前文檔的大小 49 void LogOriginalSize(); 50 } 51 52 public class Document : IStorableCompressible 53 { 54 //儲存IStorable的Status屬性的資料 55 private int status = 0; 56 57 public Document(string s) 58 { 59 Console.WriteLine("Creating Document with:{0}", s); 60 } 61 62 //實現Read方法 63 public void Read() 64 { 65 Console.WriteLine("Implementing the Read Method for IStorable"); 66 } 67 68 //實現Write方法 69 public void Write(Object obj) 70 { 71 Console.WriteLine("Implemeting the Write Method for IStorable"); 72 } 73 //實現屬性 74 public int Status 75 { 76 get 77 { 78 return status; 79 } 80 set 81 { 82 status = value; 83 } 84 } 85 86 //實現ICompressible 87 public void Compress() 88 { 89 Console.WriteLine("Implementing Compress"); 90 } 91 public void Decompress() 92 { 93 Console.WriteLine("Implementing Decompress"); 94 } 95 96 //實現ILoggedCompressible 97 public void LogSavedBytes() 98 { 99 Console.WriteLine("Implementing LogSavedBytes");100 }101 102 //實現IStorableCompressible103 public void LogOriginalSize()104 {105 Console.WriteLine("Implementing LogOriginalSize");106 }107 }108 109 //測試介面110 public class Tester111 {112 static void Main()113 {114 Document doc = new Document("Test Document!");115 116 //將Document轉換為各種介面進行操作117 IStorable isDoc = doc as IStorable;118 if (isDoc != null)119 {120 isDoc.Read();121 }122 else123 {124 Console.WriteLine("IStorable not supported");125 }126 127 ILoggedCompressible ilcDoc = doc as ILoggedCompressible;128 if (ilcDoc != null)129 {130 //ILoggedCompressible可以調用ICompress介面的方法,因為擴充了該介面131 ilcDoc.Compress();132 ilcDoc.LogSavedBytes();133 }134 else135 {136 Console.WriteLine("ILoggedCompressible not supported");137 }138 139 Console.ReadKey();140 141 }142 }143 }
View Code
//將Document轉換為各種介面進行操作 IStorable isDoc = doc as IStorable;
這裡的意思是如果不確定類是否實現了某個特定的介面,可以使用as操作符進行轉換,然後測試轉換的結構是否為null,這樣就不用冒著引起異常的風險假定已經轉換成功了。當然也可以寫成這個樣子:
IStorable isDoc = (IStorable)doc;
介面與抽象類別的比較:
因為C#不支援多繼承,所以介面更好些。但是
1.飛機會飛,鳥會飛,他們都繼承了同一個介面“飛”;但是F22屬于飛機抽象類別,鴿子屬於鳥抽象類別。
2. 就像鐵門木門都是門(抽象類別),你想要個門我給不了(不能執行個體化),但我可以給你個具體的鐵門或木門(多態);而且只能是門,你不能說它是窗(單繼承);一個門可以有鎖(介面)也可以有門鈴(多實現)。 門(抽象類別)定義了你是什麼,介面(鎖)規定了你能做什麼(一個介面最好只能做一件事,你不能要求鎖也能發出聲音吧(介面汙染))。
Programming C#.Interfaces