XuGang

记录一个程序员的成长

 

委托的两个实例


委托实例

该实例代码中,首先声明了一个delegate bool CompareOp(object lhs, object rhs)委托,再说说委托:

委托机制是促使事件发送与事件接受的一种对接策略,对象对周围信号的反应或在一定环境中所具备的对其它对象的通知行为的响应则被描述成所谓的“事件”,这可以类比人对周围世界反馈产生信号的能力。委托就是一种定向信号流:指定产生、接受信号者并产生信号反馈的技术。那么这段委托是怎么把程序连动起来的呢,看后面的代码。

首先是CompareOp employeeCompareOp = new CompareOp(Employee.RhsIsGreater)这里就象定义了一个监听装置,一旦发生了CompareOp(object lhs, object rhs)这个事件就去执行Employee.RhsIsGreater的代码。

接下来我们就去看看Employee.RhsIsGreater里面的东西。

using System;
namespace Wrox.ProfCSharp.AdvancedCSharp
{
    
delegate bool CompareOp(object lhs, object rhs); //声明委托

    
class MainEntryPoint
    {
        
static void Main()
        {
            Employee [] employees 
=
            {
                
new Employee("Karli Watson"20000),
                
new Employee("Bill Gates"10000),
                
new Employee("Simon Robinson"25000),
                
new Employee("Mortimer", (decimal)1000000.38),
                
new Employee("Arabel Jones"23000),
                
new Employee("Avon from Blake'"50000)};

            CompareOp employeeCompareOp 
= new CompareOp(Employee.RhsIsGreater);//实例化委托

            BubbleSorter.Sort(employees, employeeCompareOp);

            
for (int i=0 ; i<employees.Length ; i++)
                Console.WriteLine(employees[i].ToString());
            Console.ReadLine();
        }
    }

    
class Employee // : object
    {
        
private string name;
        
private decimal salary;

        
public Employee(string name, decimal salary)
        {
            
this.name = name;
            
this.salary = salary;
        }

        
public override string ToString()
        {
            
return string.Format(name + ", {0:C}", salary);
        }

        
public static bool RhsIsGreater(object lhs, object rhs)
        {
            Employee empLhs 
= (Employee) lhs;
            Employee empRhs 
= (Employee) rhs;
            
return (empRhs.salary > empLhs.salary) ? true : false;
        }
    }

    
class BubbleSorter
    {
        
static public void Sort(object [] sortArray, CompareOp gtMethod)
        {
            
for (int i=0 ; i<sortArray.Length ; i++)
            {
                
for (int j=i+1 ; j<sortArray.Length ; j++)
                {
                    
if (gtMethod(sortArray[j], sortArray[i])) //调用委托
                    {
                        
object temp = sortArray[i];
                        sortArray[i] 
= sortArray[j];
                        sortArray[j] 
= temp;
                    }
                }
            }
        }
    }
}


委托实例二
using System;
namespace cmdText
{
    
/**//// <summary>
    
/// 
    
/// 把委托作为参数来传递
    
/// </summary>
    
/// 
    public delegate int PrintMsgHandler(string msg);
    
    
public class PrintProvider1
    {       
        
public static int Print(string msg)
        {
            Console.WriteLine(
"打印1:"+msg);          
            
return msg.Length;
        }

        
public static void Print(PrintMsgHandler prn)
        {
            prn(
"---------Print---------------");
        }
    }

    
public class PrintProvider2
    {
        
public static int Print(string msg)
        {           
            Console.WriteLine(
"打印2:"+msg);      
            
return msg.Length;
        }
    }

    
public class TestDelegate
    {
        
public static void Main()
        {           
            PrintMsgHandler prn1 
= new PrintMsgHandler(PrintProvider1.Print);
            
int n = prn1("委托测试1!");
            
//Console.Write(n);
            
//PrintProvider1.Print(prn1);
            
            PrintMsgHandler prn2 
= new PrintMsgHandler(PrintProvider2.Print);
            prn2(
"委托测试2!");

            Console.WriteLine(
"委托链测试");
            PrintMsgHandler prn3 
= prn1 + prn2;
            prn3(
"委托测试3");

            Console.Read();        
        }
    }
}

posted on 2008-01-16 09:18  钢钢  阅读(431)  评论(0编辑  收藏  举报

导航