使用委托数组的一个例子简介一下委托

None.gif using  System;
None.gif 
None.gif
delegate   double  DoCalculate( double  num);
None.gif 
None.gif
class  RocketCalculator
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private DoCalculate[] calculations;
InBlock.gif    
private int calculationCounter;
InBlock.gif     
InBlock.gif    
public RocketCalculator()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        calculationCounter 
= 0;
InBlock.gif        calculations 
= new DoCalculate[10];
ExpandedSubBlockEnd.gif    }

InBlock.gif     
InBlock.gif    
public void AddCalculation(DoCalculate newCalculation)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        calculations[calculationCounter] 
= newCalculation;
InBlock.gif        calculationCounter
++;
ExpandedSubBlockEnd.gif    }

InBlock.gif     
InBlock.gif    
public double StartCalculation(double tempValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Start value: {0}", tempValue);   
InBlock.gif        
for (int i = 0; i < calculationCounter; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tempValue 
= calculations[i](tempValue);       
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return tempValue;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif 
None.gif
class  Math
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static DoCalculate DoMinusTwenty = new DoCalculate(MinusTwenty);
InBlock.gif    
public static DoCalculate DoTimesTwo = new DoCalculate(TimesTwo);
InBlock.gif    
public static DoCalculate DoPlusTen = new DoCalculate(PlusTen);
InBlock.gif 
InBlock.gif    
public static double MinusTwenty(double number)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Minus twenty");
InBlock.gif        
return number - 20;
ExpandedSubBlockEnd.gif    }

InBlock.gif     
InBlock.gif    
public static double TimesTwo(double number)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Times two");
InBlock.gif        
return number * 2;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
public static double PlusTen(double number)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Console.WriteLine(
"Plus ten");
InBlock.gif        
return number + 10;
ExpandedSubBlockEnd.gif    }
  
ExpandedBlockEnd.gif}

None.gif
None.gif
public   class  Tester
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
double startValue;
InBlock.gif        
double endResult;
InBlock.gif        
string response;
InBlock.gif        
InBlock.gif        RocketCalculator calculator 
= new RocketCalculator();
InBlock.gif        
InBlock.gif        Console.Write(
"Enter start value: ");
InBlock.gif        startValue 
= Convert.ToDouble(Console.ReadLine());
InBlock.gif        Console.WriteLine(
"Create sequence of operations by repeatedly");
InBlock.gif        Console.WriteLine(
"choosing from the following options");
InBlock.gif        Console.WriteLine(
"M)inus twenty");
InBlock.gif        Console.WriteLine(
"T)imes two");
InBlock.gif        Console.WriteLine(
"P)lus ten");
InBlock.gif        Console.WriteLine(
"When you wish to perform the calculation enter C\n");
InBlock.gif        
InBlock.gif        
do
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            response 
= Console.ReadLine().ToUpper();
InBlock.gif            
switch(response)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "M":
InBlock.gif                    calculator.AddCalculation(Math.DoMinusTwenty);
InBlock.gif                    Console.WriteLine(
"Minus twenty operation added");
InBlock.gif                
break;
InBlock.gif                
case "T":
InBlock.gif                    calculator.AddCalculation(Math.DoTimesTwo);
InBlock.gif                    Console.WriteLine(
"Times two operation added");
InBlock.gif                
break;
InBlock.gif                
case "P":
InBlock.gif                    calculator.AddCalculation(Math.DoPlusTen);
InBlock.gif                    Console.WriteLine(
"Plus ten operation added");
InBlock.gif                
break;
InBlock.gif                
case "C":
InBlock.gif                    endResult 
= calculator.StartCalculation(startValue);
InBlock.gif                    Console.WriteLine(
"End result: {0}", endResult);
InBlock.gif                
break;
InBlock.gif                
default:
InBlock.gif                    Console.WriteLine(
"Invalid choice please try again");
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 while (response != "C");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
None.gif

这是这个例子的代码。(好废话哦~~)

不过还真是够长的,它做了些什么呢?模拟了火箭发射吗?哈哈,肯定不可能,还没这么强。其实也就是借用了Rocket这个大名号做了一些简单的操作(计算)。如类的名字所展示的,它包含两个主要类 -  关于火箭计算的类和用于提供数学方法的类(当然还有一个用于测试的类)。

从上看下来,第三行我们就见到了我们的主题 - 委托。这个一个委托的定义,返回值跟参数要跟使用它封装的方法一致。

然后在RocketCalculator类里我们声明了一个委托数组,在类的构造器里把它实例化为长度为10的数组(当然这个长度是任意取的)。

然后我们跳到Math类,可以看到定义了3个静态方法,并把这三个方法用3个委托封装起来了,委托的名称直观得说明了方法的用途。

在Tester类里,我们实例化了RocketCalculator类,并且定义了一个的初始值,并且使用ReadLine()等待用户为它赋值。

在do循环里,我们等待用户输入一个命令,对初始值进行操作(这里有三种操作:减20,乘2和加10)。

一旦接受了一个命令,然后把这个命令送到了我们的火箭计算器里:)(怎么送呢?如代码所示,使用calculator.AddCalculation())。AddCalculation接受到一个DoCalculate类型的委托,然后保存在委托数组里。

用户发出开始指令后,calculator.StartCalculation()接受刚开始指定的初始值就开始计算了。

恩~~~有哪些计算呢?呵呵,我们不是都已经保存在委托数组里了嘛,OK!Go!

马上,我们的结果就出来了。

其实这是对一些复杂模拟程序的简单化版本。我们有一些操作(方法),然后用委托封装了起来,然后使用统一的规格使用,挺方便的。

当然,如果是没有返回值的方法,我们可以用组播委托,那么委托数组都不用了。暂时不介绍了,下次再写:)

PS:
1.在OOP术语中,类指源代码中定义的类,而在运行时期间进行“实例化”的称作一个对象。但值得注意的是,委托没有与之相似的术语。“委托”定义和委托的“实例化”均称作委托。
2.我个人认为委托就是完成了对方法的一种封装,使其规格统一(有统一的名称),以利于抽象。至于CallBack,我正在理解中:(。
3.这只是一个委托的一个简单介绍,并不涉及委托更深层次的问题,以后再写吧。
4.此文参考了《C# Primer Plus》( 这真是一本好书,我推荐所有C#的初学者去买,你学到的不将只是C#语法)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值