Programming C#.Interfaces
类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定。”
从抽象类继承实现了“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的更多相关文章
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- [Think In Java]基础拾遗1 - 对象初始化、垃圾回收器、继承、组合、代理、接口、抽象类
目录 第一章 对象导论第二章 一切都是对象第三章 操作符第四章 控制执行流程第五章 初始化与清理第六章 访问权限控制第七章 复用类第九章 接口 第一章 对象导论 1. 对象的数据位于何处? 有两种方式 ...
- <<Design Patterns>> Gang of Four
One:Introduction: One-1:Before delving into the some twenty pattern designs, it's necessary for ME ...
- Unity文档阅读 第一章 入门
Before you learn about dependency injection and Unity, you need to understand why you should use the ...
- Dependency Injection in ASP.NET MVC
原文引自http://www.dotnetcurry.com/ShowArticle.aspx?ID=786 1.传统三层结构,相邻两层之间交互: 2.如果使用EntityFramework则View ...
- HTML5 API's (Application Programming Interfaces)
New HTML5 API's (Application Programming Interfaces) The most interesting new API's are: HTML Geoloc ...
- Generic Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...
- 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 ...
- 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 ...
随机推荐
- 卸载mysql残留
一.在控制面板中查看是否有mysql,有则进行卸载.或执行同样版本号的mysql安装文件,选择"remove"进行卸载. 二.卸载mysql后其服务仍在,解决的方法: 点击&quo ...
- Java基础:泛型
Java的泛型是什么呢, 就是类型的參数化,这得类型包含方法參数和返回值.也就是原本该是确定类型的地方换成了变量,把类型的确定时间向后延迟了. 在之前,学过"重载"的概念,重载是什 ...
- 什么是OAuth
什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...
- 线上应用故障排查之一:高CPU占用
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
- 读书笔记--<<会说话的代码>>
三天看完一本书,说出来我都不信,不过我还真是史无前例的做到了, 现在分享一下我的收获,希望大家拍砖,共同讨论一下. <<会说话的代码>>一书是我们BJDP小组里的王洪亮老师的一 ...
- 记一次dedeCMS网站搭建全过程
Step 1 使用阿里云Windows Server 2012服务器 { 使用远程桌面进行操作,ip admin pwd登录 } Step 2 下载安装phpStudy包 { 下载安装,直接安装到C盘 ...
- skynet的流程1
logpath = "."harbor = 1address = "127.0.0.1:2526"master = "127.0.0.1:2013&q ...
- SPI Flash
使用了MX25L512的SPI接口的Flash 电路连接图: 总的大小512kb,即64kB,sector的大小为256 Bytes,block的大小为4k Bytes 调试时出现的问题: 1.Fla ...
- PHP5中__call、__get、__set、__clone、__sleep、__wakeup的用法
__construct(),__destruct(),__call(),__callStatic(),__get(),__set(),__isset(),__unset(),__sleep(),__w ...
- C语言的本质(12)——指针与函数
往往,我们一提到指针函数和函数指针的时候,就有很多人弄不懂.下面详细为大家介绍C语言中指针函数和函数指针. 1.指针函数 当一个函数声明其返回值为一个指针时,实际上就是返回一个地址给调用函数,以用于需 ...