delegate与接口

C#中关于delegate的接口实现(2) 

==========================================

比较下面代码(注掉comment 1 || 注掉comment 2) 具体区别参考cs0071

 1 using  System;
 2
 3 public   delegate   void  MyDelegate();
 4
 5 interface  MyInterface2
 6 {
 7    event MyDelegate Event;
 8}

 9
10 public   class  Test2:MyInterface2
11 {
12    
13    //comment 1 starting
14    private event MyDelegate innerEvent;
15    public event MyDelegate Event
16    {
17        add
18        {
19            innerEvent += value;
20            Console.WriteLine("add");
21        }

22        remove
23        {
24            innerEvent -= value;
25            Console.WriteLine("remove");
26        }

27    }

28    public void Fire()
29    {
30        if (innerEvent != null
31        {
32            innerEvent();
33        }

34    }

35    //comment 1 ending
36
37    //comment 2 starting
38//        public event MyDelegate Event;
39//        public void Fire()
40//        {
41//            if (Event != null) {
42//                Event();
43//            }
44//        }
45    //comment 2 ending
46
47
48    public void Handler()
49    {
50        Console.WriteLine("Handler been invoked!");
51    }

52
53    public static void Main()
54    {
55        Test2 p = new Test2();
56
57        p.Event += new MyDelegate(p.Handler);
58        p.Fire();
59        p.Event -= new MyDelegate(p.Handler);
60        p.Fire();
61    }

62}

63


下面是实际中用接口调用:(其中comment 1中的部分我采用了显式实现)

 1 using  System;
 2
 3 public   delegate   void  MyDelegate();
 4
 5 interface  MyInterface2
 6 {
 7    event MyDelegate Event;
 8    void Fire();
 9}

10
11 public   class  Test2:MyInterface2
12 {
13    
14    //comment 1 starting
15    private event MyDelegate innerEvent;
16    event MyDelegate MyInterface2.Event
17    {
18        add
19        {
20            innerEvent += value;
21            Console.WriteLine("add");
22        }

23        remove
24        {
25            innerEvent -= value;
26            Console.WriteLine("remove");
27        }

28    }

29    public void Fire()
30    {
31        if (innerEvent != null
32        {
33            innerEvent();
34        }

35    }

36    //comment 1 ending
37
38    //comment 2 starting
39//    public event MyDelegate Event;
40//    public void Fire()
41//    {
42//        if (Event != null) {
43//            Event();
44//        }
45//    }
46    //comment 2 ending
47}

48
49 public   class  Test4
50 {
51    public static void Handler()
52    {
53        Console.WriteLine("Handler been invoked!");
54    }

55
56    public static void Main()
57    {
58        MyInterface2 p = new Test2();
59        p.Event += new MyDelegate(Test4.Handler);
60        p.Fire();
61        p.Event -= new MyDelegate(Test4.Handler);
62        p.Fire();
63    }

64}

65

可以看出,comment 2不能显式实现,因此,对于实现两个不同的接口的两个相同名称的事件就
只能用comment 1中的方法了。 

========================================

using System;
delegate double Function(double x); // 代理函数
class Multiplier
{
    
double factor;
    
public Multiplier(double factor)
    
{
        
this.factor = factor;
    }

    
public double Multiply(double x)
    
{
        
return x * factor;
    }

}


public interface IFun {
    
double Function(double x);
}


class funImp1 : IFun
{
    
double factor;
    
public funImp1(double factor)
    
{
        
this.factor = factor;
    }

    
public double Function(double x)
    
{
        
return x * factor;
    }

}


class funImp2 : IFun
{
    
public double Function(double x)
    
{
        
return  Math.Sin(x);
    }


}



class Test
{
    
static double Square(double x)
    
{
        Console.WriteLine(
"1  静态方法  static double Square(double x)"); 
        
return x * x;
    }

    
static double[] Apply(double[] a, Function f)
    
{


        Console.WriteLine(
"2   double[] Apply(double[] a, Function f) " + f ); 


        
double[] result = new double[a.Length];
        
for (int i = 0; i < a.Length; i++) result[i] = f(a[i]);  // 自己使用当成函数名称 
        return result;
    }




    
static double[] Apply(double[] a, IFun f)
    
{


        Console.WriteLine(
"2   double[] Apply(double[] a, Function f) " + f);


        
double[] result = new double[a.Length];
        
for (int i = 0; i < a.Length; i++) result[i] = f.Function(a[i]); // 调用接口中制定的方法 
        return result;
    }





    
static void Main()
    
{
        
double[] a = 0.00.51.0 };
        
double[] squares = Apply(a, new Function(Square));
        
double[] sines = Apply(a, new Function(Math.Sin));
        Multiplier m 
= new Multiplier(2.0);
        Function f 
= new Function(m.Multiply);
        
double[] doubles = Apply(a, f );

       
        
double[] b = 0.10.51.0 };
        doubles 
= Apply(b, f );


        IFun ifun 
= new funImp1(2);
         doubles 
= Apply(a, ifun );
         Console.WriteLine(
" "+doubles);




    }

}

=======================================

何时使用Delegate或接口

来自微软的MSDN

在以下情况下使用Delegates很有用:

  • 调用一个单一方法;
  • 一个类要进行方法规范(method specification)的多种执行;
  • 使用一个静态方法来执行规范;
  • 想获得类似事件设计的模式;
  • 调用者没有必要知道或获得方法定义的对象;
  • 执行的提供者想将规范的执行“分发(hand out)”成一些可供选择的部分;
  • 代码需要进行简单的组成部分。

接口在以下情况会很有用:

  • 规范指定一套即将被调用的方法;
  • 特别的,一个类只执行规范一次;
  • 接口的调用者想通过接口类型以获得其它接口或类。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值