.Net C# interface使用
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使用的更多相关文章
- angular2系列教程(七)Injectable、Promise、Interface、使用服务
今天我们要讲的ng2的service这个概念,和ng1一样,service通常用于发送http请求,但其实你可以在里面封装任何你想封装的方法,有时候控制器之间的通讯也是依靠service来完成的,让我 ...
- 接口--interface
“interface”(接口)关键字使抽象的概念更深入了一层.我们可将其想象为一个“纯”抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不规定方法主体.接口也包含了基本数据 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 谨慎使用Marker Interface
之所以写这篇文章,源自于组内的一些技术讨论.实际上,Effective Java的Item 37已经详细地讨论了Marker Interface.但是从整个Item的角度来看,其对于Marker In ...
- 浅析Go语言的Interface机制
前几日一朋友在学GO,问了我一些interface机制的问题.试着解释发现自己也不是太清楚,所以今天下午特意查了资料和阅读GO的源码(基于go1.4),整理出了此文.如果有错误的地方还望指正. GO语 ...
- 如何设计一门语言(七)——闭包、lambda和interface
人们都很喜欢讨论闭包这个概念.其实这个概念对于写代码来讲一点用都没有,写代码只需要掌握好lambda表达式和class+interface的语义就行了.基本上只有在写编译器和虚拟机的时候才需要管什么是 ...
- abstract与interface之房祖名张默版
最近把java基础知识拿出来看看,看到abstract与interface的时候,觉得有点模糊,好像面试官也喜欢问这个问题.我在百度了查了好长时间,觉得讲算比较清楚的是那篇讲 Door,然后想要带个报 ...
随机推荐
- 添加sql距离现在多久以前时间条件
UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(add_time)<=25200 其中now()是现在时间 add_time是其他时间点 25200:是秒,现在和ad ...
- arcpy.UpdateCursor和arcpy.da.UpdateCursor计算面积时间的比较
arcpy.UpdateCursor ####################### import arcpy from arcpy import env import os import sys f ...
- Resend a Request by fiddler
Resend a Request You can resend a request directly from the Sessions List, or save requests to resen ...
- C++ replace replace_if replace_copy replace_copy_if
#include <iostream>#include <list>#include <algorithm>#include <iterator>#in ...
- linux挂载iscsi出现的问题
挂载的时候我们可以使用永久挂载但是这里我们就需要注意了正常的永久挂载可能会导致系统起不来因为这里我们使用的是服务端的硬盘为网络设备而开机的读取中 会先读取/etc/fstab下的内容 之后再读取网络的 ...
- Ambiguous mapping. Cannot map 'appController' method
笔者最初的一套代码模板 import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; impo ...
- Linux下切换用户根目录的指令
执行 usermod -d /shares/Storage/ ftp 会将用户ftp的根目录(~位置)修改为/shares/Storage/.
- linux如何添加服务为系统服务快速启动或关闭
当在linux系统安装了一些服务比如apache,mysql,iptables等等后想快速启动或者重启 但是在使用系统启动或者关闭服务时候发现输入指令 >service httpd restar ...
- git命令自动补全
git安装好后发现命令不能自动补全于是搜了很多方法,先按博客里面的方法试了下: 1.下载配置文件 git clone git://git.kernel.org/pub/scm/git/git.git ...
- Unity 的 [HideInInspector]
[HideInInspector] public Transform t; public Transform mm; public Transform nn3; 在变量前面加入,作用:隐藏下一条在In ...