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[100]; 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[100]; 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[100]; 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[0] = "this is first test"; var aaa = dog[0]; Cat cat = new Cat(); var b = cat.Behavior();//我白天睡觉,晚上出来抓老鼠。 cat.NickName = "second"; var bb = cat.NickName; cat.Eat(); cat.Play(); cat.Drink(); cat[0] = "this is second test"; var bbb = cat[0]; Mouse mouse = new Mouse(); var c = mouse.Behavior();//我白天睡觉,晚上出来找粮食吃。 mouse.NickName = "third"; var cc = mouse.NickName; mouse.Eat(); mouse.Play(); mouse.Drink(); mouse[0] = "this is third test"; var ccc = mouse[0]; // }