using System;
using System.ServiceModel; namespace com._80community.unittest.Demo
{
public delegate void ActionDelegate(); /*
* [ServiceContract]服务协定定义
* [ServiceContract] 这个特性告诉编译器,该类型(指IAnimal)是一个服务契约
*
*/
/// <summary>
/// 接口的成员包括:方法,特性,索引器,事件,不包含字段。
/// </summary>
[ServiceContract]
public interface IAnimal
{ /*
* [OperationContract]要公开的服务方法
* []代表特性继承自System.Attribute类的类,其实特性和注释。类似,是用于描述程序集、类型、成员的“备注信息”,和注释不同的是:注释是给“人”看的,而特性是给“编译器”看的
* [OperationContract] 这个特性告诉编译器,该成员(指Function1)是一个操作契约,这样在编程的时候,用反射机制可以判断出,哪些类型标记过服务契约,哪些成员标记过操作契约,在WCF中会找到这些做服务
* [OperationContract] 他这句会执行什么代码,在WCF中,会找出所有标记OperationContract特性的成员作为服务
*/
/// <summary>
/// 接口定义方法
/// </summary>
/// <returns></returns>
[OperationContract]
string Behavior(); /// <summary>
/// 接口定义属性
/// </summary>
string NickName { get; set; } /// <summary>
/// 接口定义索引器,索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写。C#中的类成员可以是任意类型,包括数组和集合。当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作。
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
string this[int i] { get; set; } /// <summary>
/// 接口定义事件
/// </summary>
event EventHandler ActionChanged; event ActionDelegate ActionEvent; }
}
using System;

namespace com._80community.unittest.Demo
{
public class Dog : IAnimal
{
public string Behavior()
{
return "我白天睡觉,晚上看家。";
} public string NickName { get; set; } private string[] arr = new string[]; public string this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
} #region 接口定义事件的实现 public void Eat()
{
OnAction();
}
protected virtual void OnAction()
{
}
#endregion #region EventHandler public event EventHandler ActionChanged;
public void Play()
{
OnActionChanged(new EventArgs());
}
protected virtual void OnActionChanged(EventArgs e)
{
if (ActionChanged != null)
{
ActionChanged(this, e);
}
} #endregion #region delegate event
public event ActionDelegate ActionEvent;
public void Drink()
{
if (ActionEvent != null)
{
ActionEvent();
}
} #endregion
}
}
using System;

namespace com._80community.unittest.Demo
{
public class Cat : IAnimal
{
public string Behavior()
{
return "我白天睡觉,晚上出来抓老鼠。";
}
public string NickName { get; set; } private string[] arr = new string[]; public string this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
} #region 接口定义事件的实现 public void Eat()
{
OnAction();
}
protected virtual void OnAction()
{
}
#endregion #region EventHandler public event EventHandler ActionChanged;
public void Play()
{
OnActionChanged(new EventArgs());
}
protected virtual void OnActionChanged(EventArgs e)
{
if (ActionChanged != null)
{
ActionChanged(this, e);
}
} #endregion #region delegate event
public event ActionDelegate ActionEvent;
public void Drink()
{
if (ActionEvent != null)
{
ActionEvent();
}
} #endregion
}
}
using System;

namespace com._80community.unittest.Demo
{
public class Mouse : IAnimal
{
public string Behavior()
{
return "我白天睡觉,晚上出来找粮食吃。";
}
public string NickName { get; set; } private string[] arr = new string[]; public string this[int i]
{
get { return arr[i]; }
set { arr[i] = value; }
} #region 接口定义事件的实现 public void Eat()
{
OnAction();
}
protected virtual void OnAction()
{
}
#endregion #region EventHandler public event EventHandler ActionChanged;
public void Play()
{
OnActionChanged(new EventArgs());
}
protected virtual void OnActionChanged(EventArgs e)
{
if (ActionChanged != null)
{
ActionChanged(this, e);
}
} #endregion #region delegate event
public event ActionDelegate ActionEvent;
public void Drink()
{
if (ActionEvent != null)
{
ActionEvent();
}
} #endregion
}
}
[TestMethod]
public void Test9()
{
Dog dog = new Dog();
var a = dog.Behavior();//我白天睡觉,晚上看家。
dog.NickName = "first";
var aa = dog.NickName;
dog.Eat();
dog.Play();
dog.Drink();
dog[] = "this is first test";
var aaa = dog[]; Cat cat = new Cat();
var b = cat.Behavior();//我白天睡觉,晚上出来抓老鼠。
cat.NickName = "second";
var bb = cat.NickName;
cat.Eat();
cat.Play();
cat.Drink();
cat[] = "this is second test";
var bbb = cat[]; Mouse mouse = new Mouse();
var c = mouse.Behavior();//我白天睡觉,晚上出来找粮食吃。
mouse.NickName = "third";
var cc = mouse.NickName;
mouse.Eat();
mouse.Play();
mouse.Drink();
mouse[] = "this is third test";
var ccc = mouse[]; //
}

.Net C# interface使用的更多相关文章

  1. angular2系列教程(七)Injectable、Promise、Interface、使用服务

    今天我们要讲的ng2的service这个概念,和ng1一样,service通常用于发送http请求,但其实你可以在里面封装任何你想封装的方法,有时候控制器之间的通讯也是依靠service来完成的,让我 ...

  2. 接口--interface

    “interface”(接口)关键字使抽象的概念更深入了一层.我们可将其想象为一个“纯”抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不规定方法主体.接口也包含了基本数据 ...

  3. Configure a bridge interface over a VLAN tagged bonded interface

    SOLUTION VERIFIED February 5 2014 KB340153 Environment Red Hat Enterprise Linux 6 (All Versions) Red ...

  4. Create a bridge using a tagged vlan (8021.q) interface

    SOLUTION VERIFIED April 27 2013 KB26727 Environment Red Hat Enterprise Linux 5 Red Hat Enterprise Li ...

  5. Configure a bridged network interface for KVM using RHEL 5.4 or later?

    environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...

  6. Set up VLAN (802.1q) tagging on a network interface?

    SOLUTION VERIFIED October 13 2015 KB39674 KB741413 environment Red Hat Enterprise Linux 4 Red Hat En ...

  7. 谨慎使用Marker Interface

    之所以写这篇文章,源自于组内的一些技术讨论.实际上,Effective Java的Item 37已经详细地讨论了Marker Interface.但是从整个Item的角度来看,其对于Marker In ...

  8. 浅析Go语言的Interface机制

    前几日一朋友在学GO,问了我一些interface机制的问题.试着解释发现自己也不是太清楚,所以今天下午特意查了资料和阅读GO的源码(基于go1.4),整理出了此文.如果有错误的地方还望指正. GO语 ...

  9. 如何设计一门语言(七)——闭包、lambda和interface

    人们都很喜欢讨论闭包这个概念.其实这个概念对于写代码来讲一点用都没有,写代码只需要掌握好lambda表达式和class+interface的语义就行了.基本上只有在写编译器和虚拟机的时候才需要管什么是 ...

  10. abstract与interface之房祖名张默版

    最近把java基础知识拿出来看看,看到abstract与interface的时候,觉得有点模糊,好像面试官也喜欢问这个问题.我在百度了查了好长时间,觉得讲算比较清楚的是那篇讲 Door,然后想要带个报 ...

随机推荐

  1. elasticsearch _all

    在轻量搜索中,我们介绍了 _all 字段:一个把其它字段值 当作一个大字符串来索引的特殊字段. query_string 查询子句(搜索 ?q=john )在没有指定字段时默认使用 _all 字段._ ...

  2. vue cli脚手架使用

    1.安装nodejs,npm https://www.cnblogs.com/xidianzxm/p/12036880.html 2.安装vue cli sudo npm install -g @vu ...

  3. [.NET] ConfuserEx脱壳工具打包

    [.NET] ConfuserEx脱壳工具打包 ConfuserEx 1.0.0脱壳步骤        Written by 今夕何夕[W.B.L.E. TeAm] 1.先用UnconfuserEx把 ...

  4. python连接mysql数据库(MySQL)

    在介绍python在数据库中的操作之前先简单介绍点mysql中的操作语言: [root@elk-server --]# mysql -u root -p Enter password: Welcome ...

  5. smarty 对数据库的简单操作。

    smarty简单配置 -------------------------------------smarty_inc.php-------------------------------------- ...

  6. features its own

    Gulp.js features its own built-in watch() method - no external plugin required ---- However, the Arn ...

  7. Apache设置静态文件的失效时间

    步骤1:启用expires模块 [root@zlinux logs]# vim httpd.conf LoadModule expires_module modules/mod_expires.so ...

  8. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  9. RabbitMQ简单实现,exchange四种模式,持久化

    RabbitMQ目录 一.简介,简单实现二.Exchange四种类型简单介绍三.消息确认,交换机.队列及消息持久化一.简介及简单实现RabbitMQ是一个消息代理:它接受并转发消息.你可以把它当成一个 ...

  10. Java工程师学习指南第5部分:Java网络编程与NIO

    本文整理了微信公众号[Java技术江湖]发表和转载过的Java网络编程相关优质文章,想看到更多Java技术文章,就赶紧关注本公众号吧. 深度解读 Tomcat 中的 NIO 模型 [Java基本功]浅 ...