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

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

  2. 论文笔记 | Self-organized Text Detection with Minimal Post-processing via Border Learning

    论文链接:http://openaccess.thecvf.com/content_ICCV_2017/papers/Wu_Self-Organized_Text_Detection_ICCV_201 ...

  3. Linux误挂载到根目录出现问题!!!!!!!!!!!!!!!

    一.背景: 因根目录/空间不大,故而想将另一硬盘挂载到根目录下(后发现此想法很是幼稚): 二.过程: 1.成功输入命令挂载后,发现出现/上被挂了两个东西,且/下剩余空间还是原来一样大,才发现大错特错: ...

  4. Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]

    本文主要介绍Ansible的几个命令模块,包括: command - 在远程节点上执行命令 shell - 让远程主机在shell进程下执行命令 script - 将本地script传送到远程主机之后 ...

  5. bind-named

    main-book: http://www.zytrax.com/books/dns resolv.conf: http://dns-learning.twnic.net.tw/bind/intro4 ...

  6. JAVA 中 if和while的区别

    while和if本身就用法不同,一个是循环语句,一个是判断语句. if 只做判断,判断一次之后,便不会再回来了while 的话,循环,直到结果为false,才跳出来 链表的结构,要一直读下去,直到读完 ...

  7. 8086键盘输入实验——《x86汇编语言:从实模式到保护模式》读书笔记07

    1.BIOS中断 我们可以为所有中断类型自定义中断处理过程,包括内部中断.硬件中断和软中断. BIOS中断,又称BIOS功能调用,主要是为了方便地使用最基本的硬件访问功能.通常,为了区分针对同一硬件的 ...

  8. Coursera 机器学习 第7章 Support Vector Machines 学习笔记

    7 Support Vector Machines7.1 Large Margin Classification7.1.1 Optimization Objective支持向量机(SVM)代价函数在数 ...

  9. 无监督学习(Unsupervised Learning)

    无监督学习(Unsupervised Learning) 聚类无监督学习 特点 只给出了样本, 但是没有提供标签 通过无监督学习算法给出的样本分成几个族(cluster), 分出来的类别不是我们自己规 ...

  10. <meta http-equiv="X-UA-Compatible" content="IE=edge" />详解

    X-UA-Compatible是针对IE8新加的一个设置,对于IE8之外的浏览器是不识别的,这个区别与content="IE=7"在无论页面是否包含<!DOCTYPE> ...