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. Python 函数lambda(), filter(), map(), reduce()

    1 filter filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String ...

  2. 【java中的final关键字】

    转自:https://www.cnblogs.com/xiaoxi/p/6392154.html 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括成员变量 ...

  3. 了解Spring Boot的自动配置

    摘自:https://www.jianshu.com/p/ddb6e32e3faf Spring Boot的自动配置给开发者带来了很大的便利,当开发人员在pom文件中添加starter依赖后,mave ...

  4. puppeteer注入cookie然后访问页面

    var puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); const ...

  5. 本地项目文件通过git提交到GitHub上

    参考:https://blog.csdn.net/kongying19910218/article/details/50515834 步骤: 1.初始化git,假如我们要提交test文件夹下的所有目录 ...

  6. CISCO实验记录十一:switch端口安全配置

    1.启用交换机端口安全 2.限制端口最大访问量为1,超出后关闭端口 1.启用交换机端口安全 #interface gigabitEthernet 0/1 #switchport mode access ...

  7. python 中对象is和==是怎么比较的

    Python中的对象包含三要素:id.type.value.其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值.is判断的是a对象是否就是b对象,是通过id来判断的.==判断的 ...

  8. CodeWar打怪升级-Python篇

    1.  The goal of this exercise is to convert a string to a new string where each character in the new ...

  9. 004-log-common-logging,Apache整合日志框架JCL门面框架、JCL+log4j

    一.概述 Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具.它提供给中间件/日志工具开发者一个 ...

  10. 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_8.RabbitMQ研究-工作模式-发布订阅模式-生产者

    Publish/subscribe:发布订阅模式 发布订阅模式: 1.每个消费者监听自己的队列. 2.生产者将消息发给broker,由交换机将消息转发到绑定此交换机的每个队列,每个绑定交换机的队列都将 ...