委托

 今天学的委托,听的不太认真,下课费了半天劲从新听了上课的视频才有点了解了。。。  

给大家推荐个网页:  http://www.cnblogs.com/jimmyzhang/archive/2007/09/23/903360.html  介绍委托很详细。

委托:委托就是一个函数的指针也就是一个方法的引用。

用委托的目的:定义一个委托让委托指向多个方法。

定义委托需要三步:

第一步,定义委托                public delegate void stopManchineryDelegat();

第二步,将委托具体化 。声明一个委托变量,谁来进行委托 第二步只是声明了 还没有实例化呢。  public stopManchineryDelegat stopManchinery;

第三步,委托变量实例化  委托变量=new 委托类型名(委托方法),但可以直接+=来绑定方法  

                      绑定方法一: stopManchinery = new stopManchineryDelegat(folder.StopFolding);

                      绑定方法二    stopManchinery += folder.StopFolding;

另外,只有当委托时的权限和建立的委托权限相同时 在Program中才可以直接调用委托的方法。

例题。

就是让建立一个Controller类来控制三个机器类的关闭方法

在Program类里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 委托
{
    class Program
    {
        static void Main(string[] args)
        {
            FoldingMachine folder = new FoldingMachine();
            WeldingMachine welder = new WeldingMachine();
            PaintingMachine painter = new PaintingMachine();


            Controller controller = new Controller();
            controller.Folder = folder;  //给Contrller类中的floder进行赋值
            controller.Painer = painter;
            controller.Welder = welder;
            controller.stopManchinery += folder.StopFolding;
            controller.stopManchinery += welder.FinishWelding;
            controller.Add(welder.FinishWelding);

            controller.ShutDown();
            Console.ReadKey();
        }
    }
}

 

在Controller类里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 委托
{
    class Controller // 创建这个类是为了控制内三个机器类的停止方法。
    {
        private FoldingMachine folder;
        private WeldingMachine welder; // 将印刷机等类实例化,为了调用在此类中调用他们中的停止方法。
        private PaintingMachine painer;
                                                      //委托时一中数据类型,声明时要关键字+返回值类型+类型名+(参数) 是引用类型 因为他没有开辟空间而是直接指向各类的方法
        public delegate void stopManchineryDelegat(); //第一步,定义委托    //只有当委托时的权限和建立的委托权限相同时 在Program中才可以直接调用委托的方法。
        public stopManchineryDelegat stopManchinery;  //第二步,将委托具体化 。声明一个委托变量,谁来进行委托 第二步只是声明了 还没有实例化呢。
                                                      //第三步,委托变量实例化  委托变量=new 委托类型名(委托方法)。
        public stopManchineryDelegat StopManchinery   //给委托绑定方法是 类的方法的返回值类型和参数必须和定义的委托的返回值类型和参数一致
        {
            set { stopManchinery = value; }         //当委托变量时私有时,为了让Program类能调到委托变量
            get { return stopManchinery; }
        }

        public void Add(stopManchineryDelegat stopMethod) //为了在外面添加方法。
        {
            stopMethod += stopMethod;
        }
        public void SetStopManchinery()
        {
            //stopManchinery = new stopManchineryDelegat(folder.StopFolding);//委托的方法一 //让具体化后的委托执行folder的关闭方法  //这就是第三步。
            stopManchinery += folder.StopFolding;
            stopManchinery += welder.FinishWelding;   //将委托绑定多个方法方法   //指向多个方法就叫多播委托
            stopManchinery += painer.PaintOff;        //委托的方法二、
            stopManchinery -= welder.FinishWelding; // -=就是解除绑定
        }

        public void ShutDown()          //控制三个类关闭的方法
        {
            //folder.StopFolding();
            //welder.FinishWelding();  //调用各机器类的方法
            //painer.PaintOff();
            //Console.ReadKey();
            SetStopManchinery();       // 设置委托都干什么 
            stopManchinery();      //执行委托所绑定的方法
        }

 

        public FoldingMachine Folder
        {
            set { this.folder = value; }   //将从外部进行赋值folder
        }
        public WeldingMachine Welder
        {
            set { this.welder = value; }
        }
        public PaintingMachine Painer
        {
            set { this.painer = value; }
        }

      

    }

 

FoldingMachine类  三个机器类代码一样 只写一个概括

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 委托
{
    class FoldingMachine
    {
        public void StopFolding()
        {
            Console.WriteLine("折叠机停止工作!");
        }
    }
}


 

posted on 2012-09-06 20:57  gongth_12  阅读(148)  评论(0编辑  收藏  举报

导航