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. mybatis中foreach参数过多效率很慢的优化

    foreach 后面in 传入的参数有1万条,#和$是有效率区别的,$的效率远高于#,上篇文章做了比较. 但没达到我的理想结果. 1. 更改方式,把foreach 去掉,改成拼装方式, 参数直接拼装成 ...

  2. 配置连接远程ip的Redis

    1.修改redis服务器的配置文件redis.windows.conf 注释绑定的主机地址 # bind 127.0.0.1 修改redis的守护进程为no,不启用 daemonize "n ...

  3. Vue 目录

    什么是 vue-cli 通过 vue-cli 建立出来的 vue

  4. flutter 中 List 和 Map 的用法

    list集合 在Dart中,数组是List对象,因此大多数人只是将它们称为List.以下是一个简单的Dart的List: 创建一个int类型的list List list = [10, 7, 23]; ...

  5. [SpringBoot/SpringMVC]从Webapp下载一个大文件出现java.lang.OutOfMemoryError: GC overhead limit exceeded怎么办?

    本文示例工程下载:https://files.cnblogs.com/files/xiandedanteng/WebFileDownload20191026.rar 制作一个Webapp,让其中一个网 ...

  6. 【java】[sql]使用Java程序向MySql数据库插入一千万条记录,各种方式的比较,最后发现insert批量插入方式对效率提升最明显

    我的数据库环境是mysql Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper 这个数据库是安装在T440p的虚拟机 ...

  7. [maven]scope之test

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...

  8. 快速根据注释生成接口文档网页工具——Apidoc的使用教程

    1,安装Node.js的npm工具环境: 如有不懂,请看我的博客:“https://blog.csdn.net/sinat_28371057/article/details/81612661“ 2,n ...

  9. CentOS7做ssh免密登录

    (1)实验环境 两台CentOS7: youxi1 192.168.1.6 youxi2 192.168.1.7 这里我将防火墙关闭进行实验,如果防火墙开启,请将端口加入到防火墙规则中. (2).目标 ...

  10. Tomcat 80端口被占用

    1.“运行”中输入cmd2.在命令行中输入netstat -ano,得到端口号对应的PID 3.打开任务管理器,点击“查看“菜单,选择“选择列”,给进程列表中添加”PID“列,然后找到PID对应的进程 ...