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.

  1. Dog kirby = new Dog("Kirby", );
  2. Seal sparky = new Seal("Sparky");
  3. DrillSergeant sarge = new DrillSergeant("Sgt. Hartman", "Tough as nails");
  4.  
  5. List<IBark> critters = new List<IBark>(){kirby, sparky, sarge};
  6.  
  7. // Tell everyone to bark
  8. foreach (IBark barkingCritter in critters)
  9. {
  10. barkingCritter.Bark();
  11. }

原文地址:#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. 基于Map的用户注册、登录、抽奖系统

    期望功能 方法设计 1.类的设计: 定义两个参与者的属性: private String username; private String userpassword; 2.注册方法: public v ...

  2. hcheck 脚本

    hcheck.sql - Script to Check for Known Problems in Oracle8i, Oracle9i, Oracle10g, Oracle 11g and Ora ...

  3. Kafka使用多个分区时 consumer的Assign配置

    天天在给自己挖坑排坑... 因为要开多线程消费,所以分区加到了10,两个broker. Producer没有做特殊处理,所以是随机发到Partitions. 但是Consumer只做Subscribe ...

  4. Single Vendor Project in OpenStack

    1.astara: ptl: name: Ryan Petrello irc: ryanpetrello email: ryan.petrello@dreamhost.com irc-channel: ...

  5. 如何发布一个包到npm && 如何使用自己发布的npm包 && 如何更新发布到npm的package && 如何更新当前项目的包?

    如何发布一个包到npm First 在https://www.npmjs.com注册一个账号. Second 编辑好项目,文件大致如下: 其中,gitignore可以如下: .DS_Store nod ...

  6. 关于kernel的疑问,不解

    (1)最近看的关于linux的部分主要是底层部分,linux对设备是如何访问,结果还是通过地址.机制差不错知道,下一步是阅读相关代码进一步理清内核,同时了解驱动的开发. 主要的疑问是对于高端内存映射, ...

  7. Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决

    在将spring的xml配置改为java配置的过程中,遇到了一些问题,block时间比较长的一个问题是资源(.xml, .properties)的路径找不到,最后是使用PathMatchingReso ...

  8. 线程同步(windows平台):互斥对象

    一:介绍 互斥对象是系统内核维护的一种数据结构,保证了对象对单个线程的访问权. 二:函数说明 创建互斥对象:    HANDLE CreateMutex(            LPSECURITY_ ...

  9. oracle 查询及删除表中重复数据

    create table test1( id number, name varchar2(20) ); ,'jack'); ,'jack'); ,'peter'); ,'red'); insert i ...

  10. Linux IO

    Linux 系统编程(IO) 工具 strace: 根据系统调用 od -tcx: 查看二进制 函数参数 使用const修改的指针为传入参数 不使用const的指针为传出参数 string操作的函数 ...