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

从抽象类继承实现了“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. CheckBoxPreference组件

    CheckBoxPreference 选中为true 取消选中为false 它的值会以boolean的形式储存在SharedPreferences中. <?xml version="1 ...

  2. vs2010安装svn插件

    vs2010安装svn插件及简单使用 1.下载安装程序,安装 2.配置vs2010 3.check out工程 1.下载安装程序,安装 2.配置vs2010 3.check out工程 Open th ...

  3. 详解Linq to SQL

    第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并 ...

  4. Hadoop 安装(3) JDK 的安装

    使用vsftp上传,JDK 和 Hadoop 到 Hadoop 用户目录. 安装JDK root 身份登录Slave4.Hadoop,在 /usr 下创建 "Java"文件夹,将j ...

  5. SSAS中CUBE行权限数据级权限控制

    去年做了一个数据仓库的项目,其中涉及到了CUBE数据级权限的控制.在网上找这方面的资料,找到一个[BI] 通用数据级权限控制解决方案的实现(二):Cube中的角色设置与数据级权限控制.根据这个大牛的思 ...

  6. xcode新建项目介绍

    xcode新建项目介绍 1.打开xcode选择“create a new xcode project 2.product name 工程名称 campany identifter 公司id 一般都写公 ...

  7. effective c++ 条款06 不想自动生成函数,就明确拒绝

    编辑器会主动的生成三个/五个函数,如果不需要我们应该主动拒绝 使用私有属性来拒绝 ``` include int main() { return 0; } ``` 使用继承的方式来拒绝

  8. mysql优化(2) 观察mysql进程状态

    环境准备,安装sysbench wget http://nchc.dl.sourceforge.net/project/sysbench/sysbench/0.4.12/sysbench-0.4.12 ...

  9. Swift笔记3

    赋值运算符" = " let (x,y) =(10,45) var str = "luo" + "shaui"    //会得到luoshu ...

  10. linux ARP攻击处理

    今天部门受到arp攻击 多说机器无法正常联网了,windows下的绑定下mac地址或者打开360arp防火墙就就ok了.我讲讲linux下的arp攻击的发现和处理吧.边学边讲,说的不对的欢迎大家指出, ...