类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定。”
从抽象类继承实现了“is-a(是一种)”关系,实现接口时一种“implement(实现)”关系,区别在于:
举个例子:
汽车是一种运载工具,它可以实现CanBeBoughtWithABigLoan(可贷巨款购买)这种能力(就像房子一样)
/*************************************************************************************
*
* 1.如果定义一个Document类,这个类可以存储,又可以压缩,所以要同时实现IStorable和ICompressible接口
* 2.扩展接口就是用一个新街口扩展原来的接口,通过扩展接口我们表示了这样的意思:
* 实现了新接口的任何东西也必须实现原来的接口
* 3.组合接口就是将已有的接口组合起来,并且可以增加新的方法或者属性来创建新的接口
*
*
*
*
*
*
*
*
*
*************************************************************************************/
using System; namespace SimpleInterface { //第一个接口 interface IStorable { //接口的方法声明中没有访问修饰符,隐含是public的,因为接口是要有其他类使用的协定 void Read(); void Write(Object obj); //属性的声明并未实现get和set方法,只是声明这两个方法 int Status { get; set; } } //第二个接口 interface ICompressible { void Compress(); void Decompress(); } //扩展接口 interface ILoggedCompressible : ICompressible { //新接口增加了一个新的方法记录节省的字节数 void LogSavedBytes(); } //组合接口 interface IStorableCompressible : IStorable, ILoggedCompressible { //存储压缩前文档的大小 void LogOriginalSize(); } public class Document : IStorableCompressible { //存储IStorable的Status属性的数据 private int status = 0; public Document(string s) { Console.WriteLine("Creating Document with:{0}", s); } //实现Read方法 public void Read() { Console.WriteLine("Implementing the Read Method for IStorable"); } //实现Write方法 public void Write(Object obj) { Console.WriteLine("Implemeting the Write Method for IStorable"); } //实现属性 public int Status { get { return status; } set { status = value; } } //实现ICompressible public void Compress() { Console.WriteLine("Implementing Compress"); } public void Decompress() { Console.WriteLine("Implementing Decompress"); } //实现ILoggedCompressible public void LogSavedBytes() { Console.WriteLine("Implementing LogSavedBytes"); } //实现IStorableCompressible public void LogOriginalSize() { Console.WriteLine("Implementing LogOriginalSize"); } } //测试接口 public class Tester { static void Main() { Document doc = new Document("Test Document!"); //将Document转换为各种接口进行操作 IStorable isDoc = doc as IStorable; if (isDoc != null) { isDoc.Read(); } else { Console.WriteLine("IStorable not supported"); } ILoggedCompressible ilcDoc = doc as ILoggedCompressible; if (ilcDoc != null) { //ILoggedCompressible可以调用ICompress接口的方法,因为扩展了该接口 ilcDoc.Compress(); ilcDoc.LogSavedBytes(); } else { Console.WriteLine("ILoggedCompressible not supported"); } Console.ReadKey(); } } } //将Document转换为各种接口进行操作 IStorable isDoc = doc as IStorable;
这里的意思是如果不确定类是否实现了某个特定的接口,可以使用as操作符进行转换,然后测试转换的结构是否为null,这样就不用冒着引起异常的风险假定已经转换成功了。当然也可以写成这个样子:
IStorable isDoc = (IStorable)doc;
接口与抽象类的比较:
因为C#不支持多继承,所以接口更好些。但是
1.飞机会飞,鸟会飞,他们都继承了同一个接口“飞”;但是F22属于飞机抽象类,鸽子属于鸟抽象类。2. 就像铁门木门都是门(抽象类),你想要个门我给不了(不能实例化),但我可以给你个具体的铁门或木门(多态);而且只能是门,你不能说它是窗(单继承);一个门可以有锁(接口)也可以有门铃(多实现)。 门(抽象类)定义了你是什么,接口(锁)规定了你能做什么(一个接口最好只能做一件事,你不能要求锁也能发出声音吧(接口污染))。