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 ...
随机推荐
- JavaScript中模块“写法”
在JavaScript模块到底是什么 event = function() { // do more return { bind: function() {}, unbind: function() ...
- mysql jdbc 查询连接问题
做了一个测试,mysql jdbc 链接A调用setAutoCommit,设置false,查询指定数据,可以查询出来,另个一链接把指定的数据给删除了,第一个链接在此查询的时候,仍然可以查询出来,使用的 ...
- javascript——touch事件介绍与实例演示
分类: javascript2014-02-12 16:42 1742人阅读 评论(0) 收藏 举报 touch事件touchmovetouchstarttouchend 前言 诸如智能手机和平板 ...
- Ext.grid.GridPanel的属性
1.Ext.grid.GridPanel 主要配置项: store:表格的数据集 columns:表格列模式的配置数组,可自动创建ColumnModel列模 ...
- node.js安装以及相关配置
安装: 首先需要进行安装.关于如何安装Node.js,这里就不赘述了,可以直接参考官方的安装指南.安装到指定盘后(以下内容以D盘为例),就开始进行相关配置. 配置: 首先配置环境变量:我的电脑--&g ...
- C++结构体中sizeof(1)
sizeof sizeof操作符的作用是返回一个对象或类型名的长度,长度的单位是字节. 返回值的类型是标准库命名为size_t的类型,size_t类型定义在cstddef头文件中,该头文件是C标准库的 ...
- Android EditText屏蔽默认长按粘贴复制事件
et.setCustomSelectionActionModeCallback(new ActionMode.Callback()); 添加全部的方法即可,不需要任何改动.
- Almost Prime
Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...
- flex lineChart中自定义datatip
原文 http://www.giser.net/?p=776 在Flex4中使用lineChart会遇到一个bug,datatip上的背景是黑色的,造成文字看不清楚,和整体界面不协调. 那么解决这个问 ...
- RBF network
1.radial basis function RBF表示某种距离,$\mu_m$为中心点,相当于将点$x$到中心点的某种距离作为特征转换 Output方法可以根据需求任意选取(比如使用SVM,log ...