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 ...
随机推荐
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...
- BZOJ 3674 可持久化并查集加强版 可持久化并查集
题目大意:同3673 强制在线 同3673 仅仅只是慢了一些0.0 这道题仅仅写路径压缩比仅仅写启示式合并要快一点点 两个都写就慢的要死0.0 改代码RE的可能是内存不够 #include<cs ...
- Android OpenCV实现图片叠加,水印
关于如何用纯OpenCV实现图片叠加的例子实在是太少,太多的是使用 C++,JNI实现的,如果要用C++的话,我们为啥不转行做C++ 下面的例子基于 Android JavaCV 实现了在im_bea ...
- Java File类读取文件属性
package myjavademo;import java.io.*; publicclass MyJavaDemo { public static void main(String[] ...
- 性能强悍的开源关系数据库PostgreSQL
性能强悍的开源关系数据库PostgreSQL
- 样式布局分享-基于frozen.js的移动OA
最近改造了公司移动OA的样式,改善了显示效果和用户体验,其中用到了腾讯的 frozen.js 框架,在此把布局代码分享给大家,希望大家可以用得上,具体效果如下图. Demo 百度网盘 下载地址 Git ...
- WCF Test Client
WCF测试客户端(WCF Test Client)是一个用来测试WCF服务程序的调试工具,能够使开发WCF服务更加方便. 在Visual Studio之外打开WCF测试客户端有两种方法:第一种方法是到 ...
- Molecule to atoms
For a given chemical formula represented by a string, count the number of atoms of each element cont ...
- Spring XML 不给提示
a)window – preferences – myeclipse – xml – xml catalog b)User Specified Entries – add i. Location: D ...
- json对象和json字符串转换方法
在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. 先明 ...