类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定。”

从抽象类继承实现了“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 = ; 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. 就像铁门木门都是门(抽象类),你想要个门我给不了(不能实例化),但我可以给你个具体的铁门或木门(多态);而且只能是门,你不能说它是窗(单继承);一个门可以有锁(接口)也可以有门铃(多实现)。 门(抽象类)定义了你是什么,接口(锁)规定了你能做什么(一个接口最好只能做一件事,你不能要求锁也能发出声音吧(接口污染))。

Programming C#.Interfaces的更多相关文章

  1. Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...

  2. [Think In Java]基础拾遗1 - 对象初始化、垃圾回收器、继承、组合、代理、接口、抽象类

    目录 第一章 对象导论第二章 一切都是对象第三章 操作符第四章 控制执行流程第五章 初始化与清理第六章 访问权限控制第七章 复用类第九章 接口 第一章 对象导论 1. 对象的数据位于何处? 有两种方式 ...

  3. <<Design Patterns>> Gang of Four

    One:Introduction: One-1:Before  delving into the  some twenty pattern designs, it's necessary for ME ...

  4. Unity文档阅读 第一章 入门

    Before you learn about dependency injection and Unity, you need to understand why you should use the ...

  5. Dependency Injection in ASP.NET MVC

    原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786 1.传统三层结构,相邻两层之间交互: 2.如果使用EntityFramework则View ...

  6. HTML5 API's (Application Programming Interfaces)

    New HTML5 API's (Application Programming Interfaces) The most interesting new API's are: HTML Geoloc ...

  7. Generic Interfaces (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...

  8. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  9. Serial Port Programming on Linux(转载)

    This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little ...

随机推荐

  1. HDU2054_A == B ?【模拟题】【大数】【水的问题】

    A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. Object-c学习之路七(oc字符串操作)

    // // main.m // NSString // // Created by WildCat on 13-7-25. // Copyright (c) 2013年 wildcat. All ri ...

  3. Linux下卸载ORACLE的多种方法(转)

    第一种# cd /u01/app/oracle/product/11.2.0/client_1/deinstall/ # ./deinstall# rm -rf /u01/app/oracle# rm ...

  4. css系列教程--css文件的创建

    css文件的创建:1.外部样式表:<link rel="stylesheet" type="text/css" href="mystyle.cs ...

  5. android欢迎页源码

    直接上源码: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import ...

  6. OC——封装(初级与高级)

    所谓的封装,就是通过定义方法或者函数去操作成员属性或者成员变量,而不是直接通过指针方式去操作.借此达到提高代码安全性,代码可行性以及代码执行效率的目的. 1:初级封装,对成员变量进行封装. #impo ...

  7. Java 四种线程池的使用

    java线程线程池监控 Java通过Executors提供四种线程池,分别为: newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...

  8. 设计模式之适配器模式(Decorator)

    1.意图 动态地给一个对象添加一些额外的功能. 2.适用性 动态.透明的方式给单个对象添加职责. 如果不适合适用子类来进行扩展的时候,可以考虑适用装饰模式. 避免子类数目爆炸性增长. 3.结构 4.参 ...

  9. 解析处理常用json数据总结

    工作中用ajax接收到接口返回的数据需要进行解析后操作,这里总结一下平时的方法,用的jquery,复制下来的页面把引入的路径改一下即可. <!DOCTYPE html> <html ...

  10. php 迭代器与和生成器

    php有很多功能强大的接口,其中ArrayAccess 与 Iterator 的配合使用可以让对象与数组一样有着灵活的访问性. 当然,用ArrayAccess 与 Iterator 配合可以用来对付数 ...