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. 安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]

    安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI] 可以使用adb install -t 解决 对于已经在手机的文件可以使用pm ...

  2. Linux设备驱动程序 之 中断下半部

    中断处理程序的局限 1. 中断处理程序以异步的方式执行,并且它有可能会打断其他重要代码的执行,因此,为了避免被打段的代码停止时间过长,中断处理程序应该执行的越快越好: 2. 如果当前有一个中断处理程序 ...

  3. js返回函数, 函数名后带多个括号的用法及join()的注意事项

    内容 题目描述 输入 输出 题目描述 实现函数 functionFunction,调用之后满足如下条件: 1.返回值为一个函数 f 2.调用返回的函数 f,返回值为按照调用顺序的参数拼接,拼接字符为英 ...

  4. 使用apache commons net进行ftp传输

    apache commons net的maven地址: http://mvnrepository.com/artifact/commons-net/commons-net/3.6 <!-- ht ...

  5. Linux磁盘和文件系统扩容彻底研究

    1.物理卷: LVM 逻辑卷的底层物理存储单元是一个块设备,比如一个分区或整个磁盘.要在 LVM 逻辑卷中使用该设备,则必须将该设备初始化为物理卷(PV). 2.卷组:物理卷合并为卷组(VG).这样就 ...

  6. docker attach 和 exec 用法区别

    attach 用法 $ sudo docker attach 665b4a1e17b6 #by IDor$ sudo docker attach loving_heisenberg #by Name$ ...

  7. 最新 央视网java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.央视网等10家互联网公司的校招Offer,因为某些自身原因最终选择了央视网.6.7月主要是做系统复习.项目复盘.LeetCo ...

  8. 最新 快乐阳光java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.快乐阳光等10家互联网公司的校招Offer,因为某些自身原因最终选择了快乐阳光.6.7月主要是做系统复习.项目复盘.Leet ...

  9. JavaScript 短路值

    了解表达式中的短路值. 逻辑运算从左到右.逻辑或运算,当左边的条件成立时,后面的条件将不再参与运算.因此在逻辑或运算中,尽量将条件结果为true的放第一位.而在逻辑与运算中,尽量将条件结果为false ...

  10. nssm设置solr开机启动服务

    首先,下载nssm http://www.nssm.cc/download 命令 nssm install solr 然后到服务里启动solr,并设置为自动 Ctrl+Shift+Esc(说明:Esc ...