You might wonder why you'd define an interface, have a class implement that interface and then access the class through the interface instead of just using the class directly.

One benefit of interface is that it allows you to treat different types of objects in the same way, provided that they implement a common interface.

For example, assume that we have Dog, Seal and DrillSergeant classes, all of which implement the IBark interface (which contains a Bark method). We can now store a collection of instances of these classes and ask all objects in our collection to Bark by using the IBark interface.

 Dog kirby = new Dog("Kirby", );
Seal sparky = new Seal("Sparky");
DrillSergeant sarge = new DrillSergeant("Sgt. Hartman", "Tough as nails"); List<IBark> critters = new List<IBark>(){kirby, sparky, sarge}; // Tell everyone to bark
foreach (IBark barkingCritter in critters)
{
barkingCritter.Bark();
}

原文地址:#438 - Benefits of Using Interfaces

【转载】#438 - Benefits of Using Interfaces的更多相关文章

  1. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  2. Architecture.SOLID-Principles

    SOLID Principles Reference 1. Single Responsibility http://en.wikipedia.org/wiki/Single_responsibili ...

  3. 你应当如何学习C++(以及编程)(转载)

    你应当如何学习C++(以及编程)(rev#1) By 刘未鹏(pongba) C++的罗浮宫(http://blog.csdn.net/pongba) Javascript是世界上最受误解的语言,其实 ...

  4. GJM :异步Socket [转载]

    原帖地址:http://blog.csdn.net/awinye/article/details/537264 原文作者:Awinye 目录(?)[-] 转载请原作者联系 Overview of So ...

  5. RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

    前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...

  6. RS-232 vs. TTL Serial Communication(转载)

    RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用U ...

  7. 【转载】PHP使用1个crontab管理多个crontab任务

    转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...

  8. [转载] Android.Hook框架xposed开发篇

    本文转载自: http://www.52pojie.cn/thread-396793-1-1.html 原帖:http://drops.wooyun.org/tips/7488 作者:瘦蛟舞 官方教程 ...

  9. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

随机推荐

  1. linux下的常见信号总结

    在linux下有很多信号,按可靠性分为可靠信号和非可靠信号,按时间分为实时信号和非实时信号,linux进程也有三种方式来处理收到的信号: (1)忽略信号,即对信号不做任何处理,其中,有两个信号不能忽略 ...

  2. bat mvn Maven中-DskipTests和-Dmaven.test.skip=true的区别 Maven 生命周期

    cd C:\Users\lt32806\git\tempest call mvn clean compile -Dmaven.test.skip=truepause Maven中-DskipTests ...

  3. PIE SDK小波变换

    1.算法功能简介 小波变换是一种信号的时间——尺度分析方法,具有多分辨率分析的特点,而且在时频两域都具有表征信号局部特征的能力,是一种窗口大小固定不变但其形状可变,时间窗和频率窗都可变的时频局部化分析 ...

  4. Google Authenticator(谷歌身份验证器)

    <!DOCTYPE html>Google Authenticator(谷歌身份验证器) ] Google Authenticator(谷歌身份验证器) Google Authentica ...

  5. jumpserver 安装详解

    一,下载软件 下载前安装依赖软件 yum install -y epel-release                        yum -y install git python-pip my ...

  6. 安装Newton版Swift,配合keystone认证

    主控节点安装配置 Keystone 认证服务 主控节点 IP:192.168.81.11 yum install centos-release-openstack-newton -y yum upda ...

  7. (转)Shell中获取字符串长度的七种方法

    Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...

  8. 在Spark shell中基于Alluxio进行wordcount交互式分析

    Spark是一个分布式内存计算框架,可部署在YARN或者MESOS管理的分布式系统中(Fully Distributed),也可以以Pseudo Distributed方式部署在单个机器上面,还可以以 ...

  9. 02-struts2结果常见的四种处理方式

    1 转发 <!--转发 --> <action name="Demo1Action" class="www.test.a_result.Demo1Act ...

  10. ArcGIS发布地图服务后直接调用查看方法

    做项目配置了一个地理底图,不知道有没有问题,如何给到客户查看并确认呢? 首先在ArcGIS上发布该地图: 打开地图后->文件->共享为->服务…… 发布成功后,得到可以访问的地图服务 ...