c#接口,代理和事件

C#接口,代理和事件

1 c#接口

接口定义的语句格式

【代码属性】【修饰符】 interface 接口名【:基接口列表】

{ 。。。//接口成员定义体

}

[Author(“tang wei wei”]//自定义属性,说明接口的设计者是唐伟伟

interface IMyExample {

 string this[int index] { get ; set ; }

 event EventHandler Even ;  //事件成员

 void Find(int value) ;  

 string Point { get ; set ; }

}

public delegate void EventHandler(object sender, Event e) ;

 

上面例子中的接口包含一个索引this、一个事件Even、一个方法Find和一个属性Point

下面看俩个示例

using System;

namespace Iface

{

   public interface IFather  //接口Ifather

   {

       int x {get;set;}

       int y {get;set;}

       void Play();

       void Clear();

   }

   class CFather:IFather     //CFather

   {

       public int x //实现接口IFather中的属性x

       {

          get

          {

              Console.WriteLine("CFather类中实现属性x");

              return 0;

          }

          set

          {

              Console.WriteLine("CFather类中实现属性x");

          }

       }

       public int y

       {

          get

          {

              Console.WriteLine("CFather类中实现属性y");

              return 0;

          }

          set

          {

              Console.WriteLine("CFather类中实现属性y");

          }

       }

       public void Play()

       {

          Console.WriteLine("CFather类实现方法Play()方法");

       }

       public void Clear()

       {

          Console.WriteLine("CFather类中实现了Clear()方法");

       }

   }

   class CSon: CFather,IFather

   {

       private int mx;

       private int my;

       private string mtext;

       new public void Play()      //重新实现接口IFather中的定义的Play()方法

       {

          Console.WriteLine("CSon类中实现Play()方法");

       }

       public string Text

       {

          get

          {

              return mtext;

          }

          set

          {

              mtext=value;

          }

       }

       new public void Clear()

       {

          this.Text="";

          Console.WriteLine("CSon类中实现Clear()方法");

       }

   }

   class App

   {

       public static void Main (string[] args)

       {

          CSon ctext=new CSon();

          Console.WriteLine("通过类CSon的实例调用接口成员");

          ctext.x=0;//由于类CSon类中没有实现属性x,所以调用其基类CFather中的x属性实现

          ctext.y=0;//同上

          ctext.Play();

          ctext.Clear();

          Console.WriteLine();

          Console.WriteLine("通过接口IFather的实例调用");

          IFather itext=ctext;

          itext.x=1;  //接口IFather的属性x被映射到类CFather中实现

          itext.y=1; //接口IFather的属性y被映射到类CFather中实现

          itext.Play(); //接口IFather中的Play方法被映射到派生类CSonPlay方法的实现上。

          itext.Clear();  //接口IFather中的Clear方法被映射到派生类CSonClear方法的实现上。

          Console.WriteLine();

          Console.WriteLine("通过CFather的实例调用接口成员");

            CFather obtext=ctext;

          //通过类CFather的对象调用的接口成员全部是类CFather提供的实现

          //说明CFather类的接口映射没有被CSon类的重新实现所改变

          obtext.x=1;

          obtext.y=1;

          obtext.Play();

          obtext.Clear();

       }

   }

}

示例二

using System;

interface IA

{

   void Play();

}

class Father:IA

{

   void IA.Play()

   {

       Console.WriteLine("调用虚方法VirtualPlay()");

       VirtualPlay();

   }

   public virtual void VirtualPlay()

   {

       Console.WriteLine("调用father类中的虚方法VirtualPlay()");

   }

}

class Son:Father

{

   public override void VirtualPlay()

   {

       Console.WriteLine("调用派生类的新方法VirtualPlay()");

   }

}

class App

{

   public static void Main ()

   {

       Father f=new Father();

       Son s=new Son();

       IA fa=f;

       IA sa=s;

       f.VirtualPlay();

       s.VirtualPlay();

       fa.Play();

       sa.Play();

   }

}

/*可在显式接口成员实现的过程中调用类的一个虚方法,由于可以在派生类中实现这个

方法,所以同样可以达到调用不同方法的目的*/

示例三

//定义两个接口

public interface ISwim

{

void Swim();

}

public interface IFood

{

void Cook();

}

接口可以看成是一种“纯”的抽象类,它的所有方法都是抽象方法。

可以用与继承相同的语法定义一个类实现某些接口:

//定义一个抽象类

public abstract class Bird

{

public abstract void Fly();

}

//继承自一个抽象类,实现两个接口

public class Duck : Bird, IFood, ISwim

{

//实现ISwim接口

public void Swim()

{

Console.WriteLine("是鸭子就会游泳");

}

//实现IFood接口

public void Cook()

{

Console.WriteLine("鸭子经常被烧烤,北京烤鸭就很有名");

}

//实现抽象类Bird中的抽象方法

public override void Fly()

{

Console.WriteLine("只有野鸭才会飞");

}

}

可以看到,抽象类定义了对象所属的类别,而接口实际上定义了一种对象应具有的行为

特性。

可按以下公式使用接口:

接口类型名 变量名=new 实现了接口的类型名();

示例代码如下:

static void Main (string[] args)

{

Duck d = new Duck();

//Duck对象d可以使用3种方法:

//1.自身定义的;

//2.父类定义的

//3.接口定义的

d.Fly();

d.Cook();

d.Swim();

//将子类(Duck)对象赋给基类变量

Bird b = d;

//现在只能使用基类定义的Fly()方法

b.Fly();

//Duck对象赋给ISwin接口变量

ISwim s = d;

//现在只能使用接口定义的Swim()方法

s.Swim();

//Duck对象赋给另一个实现的接口IFood接口变量

IFood f = d;

//现在只能使用接口定义的Cook()方法

f.Cook();

}

C#代理

【代码属性】【修饰符】delegate 返回类型 代理名(【形式参数列表】)

示例

using System;

class MathClass

{

    public static int max(int a,int b)

    {

     return(a>b?a:b);

    }

    public static int min(int a,int b)

    {

     return(a<b?a:b);

    }

    public static int sub(int a,int b)

    {

       return(a+b);

    }

    public static int minus(int a,int b)

    {

       return(a-b);

    }

}

class Handler

{

    private delegate int Calculation(int a,int b);

    private static Calculation[] myCalculation=new Calculation[2];

    public static void EventHandler(int i,int a,int b)

    {

       switch(i)

       {

           case 1:

              myCalculation[0]=new Calculation(MathClass.max);

               myCalculation[1]=new Calculation(MathClass.min);

              Console.WriteLine(myCalculation[0](a,b));

              Console.WriteLine(myCalculation[1](a,b));

              break;

           case 2:

              myCalculation[0]=new Calculation(MathClass.sub);

               myCalculation[1]=new Calculation(MathClass.minus);

              Console.WriteLine(myCalculation[0](a,b));

              Console.WriteLine(myCalculation[1](a,b));

              break;

           default:

               return;

       }

    }

}

class Test

{

    static void Main ()

    {

       Handler.EventHandler(1,2,3);

       Handler.EventHandler(2,3,4);

       Console.ReadKey();

    }

}

示例二

using System;

delegate void StringDelegate(ref string str);//定义一个代理

class StringOps

{

    static void ReplaceSpaces(ref string s)

    {

       Console.WriteLine("用连字符代替空格");

       s=s.Replace(' ' , '-');

    }

    static void RemoveSpaces(ref string s)

    {

       string temp="";

       int i;

       Console.WriteLine("删除空格");

       for (i=0;i<s.Length ;i++ )

       {

           if(s[i]!=' ') temp+=s[i];

       }

       s=temp;

    }

    static void Reverse(ref string s)

    {

       string temp="" ;

       int i;

       Console.WriteLine("字符串反序");

       for (i=s.Length-1;i>=0;i-- )

       {

           temp+=s[i];

       }

       s=temp;

    }

    public static void Main ()

    {

       StringDelegate strdelegate;

       StringDelegate replacesp=new StringDelegate(ReplaceSpaces);

       StringDelegate removesp=new StringDelegate(RemoveSpaces);

       StringDelegate reversestr=new StringDelegate(Reverse);

       string str="i am a teacher.";

       //创建多播,用于调用RepaceSpaces()和Reverse()方法

       strdelegate=replacesp;

       strdelegate+=reversestr;

       //调用多播

       strdelegate(ref str);

       Console.WriteLine("操作字符串结果: "+str);

       Console.WriteLine();

       strdelegate-=replacesp;

       strdelegate+=removesp;

       str="i am a teacher.";

       strdelegate(ref str );

       Console.WriteLine("操作字符串结果: "+str);

       Console.WriteLine();

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值