Delegate 示例

//Program.cs

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

namespace DelegateIsAll
{
    class program
    {
        public static void CarNeedWash(object sender, CarEventArgs e)
        {
            Console.WriteLine("{0} wash {1}", sender, e.msg);
        }
        public static void CarNeedFix(object sender, CarEventArgs e)
        {
            Console.WriteLine("{0} fix {1}", sender, e.msg);
        }

        static void Main()
        {
            Car c1 = new Car(true, true);
            c1.NeedWash = new Car.CarEventHandler(CarNeedWash);
            c1.NeedFix = new Car.CarEventHandler(CarNeedFix);

            c1.checkCar();
            Console.ReadLine();
            //if(c1.dirty)
        }
    }
}

//namespace DelegateIsAll
//{
//    public delegate void DelegateSample(string msg);
//    class program
//    {
//        static void CoutHehe(string msg)
//        {
//            Console.WriteLine(msg);
//        }
//        static void CoutHi(string msg)
//        {
//            Console.WriteLine(msg);
//        }
//        static void Main()
//        {
//            DelegateSample d1 = new DelegateSample(CoutHehe);
//            DelegateSample d2 = new DelegateSample(CoutHi);

//            d1("Hehe...");
//            d2("Hi...");
//            //d1.Invoke("He he ...");
//            //d2.Invoke("Hi....");
//            Console.ReadLine();
//        }
//    }
//}
//namespace DelegateIsAll
//{
//    public delegate void GenericDelegate<T>(T arg);
//    class program
//    {
//        static void StringSample(string arg)
//        {
//            Console.WriteLine("arg in Upper is: {0}", arg.ToUpper());
//        }
//        static void IntSample(int arg)
//        {
//            Console.WriteLine("++arg is: {0}", ++arg);
//        }
//        static void Main()
//        {
//            GenericDelegate<string> strTarget = new GenericDelegate<string>(StringSample);
//            strTarget("good good study, day day up");

//            GenericDelegate<int> intTarget = new GenericDelegate<int>(IntSample);
//            intTarget(7);

//            Console.ReadLine();
//        }

//    }
//}
//namespace DelegateIsAll
//{
//    public class Garage
//    {
//        private List<Car> theCars = new List<Car>();
//        public Garage()
//        {
//            theCars.Add(new Car("A", true, false));
//            theCars.Add(new Car("B", false,false));
//            theCars.Add(new Car("C", false, true));
//        }
//        public void ProcessCar(Car.MaintainDelegate proc)
//        {
//            foreach (Car c in theCars)
//            {
//                proc(c);
//            }
//        }
//    }
//    public class ServiceDepartment
//    {
//        public void WashCar(Car c)
//        {
//            if (c.Dirty)
//                Console.WriteLine("Please Wait for Washing....");
//            else
//                Console.WriteLine("Already clean...");
//        }
//        public void FixCar(Car c)
//        {
//            if (c.Broken)
//                Console.WriteLine("Please Wait for Fixing...");
//            else
//                Console.WriteLine("Run well...");
//        }
//    }
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            Garage g = new Garage();
//            ServiceDepartment sd = new ServiceDepartment();

//            g.ProcessCar(sd.WashCar);

//            g.ProcessCar(sd.FixCar);

//            Console.ReadLine();
//        }
//    }
//}
//namespace DelegateIsAll
//{
//    class Program
//    {
//        static void Main(string[] args)
//        {
//            Car c1 = new Car("Nini", 100, 10);

//            Car.Exploded d = new Car.Exploded(CarExploded);
//            Car.AboutToBlow a1 = new Car.AboutToBlow(CarAboutTo);
//            Car.AboutToBlow a2 = new Car.AboutToBlow(CarIsAlmost);
//            //c1.OnAboutToBlow(new Car.AboutToBlow(CarIsAlmost));
//            //c1.OnAboutToBlow(new Car.AboutToBlow(CarAboutTo));
//            c1.OnAboutToBlow(a1);
//            c1.OnAboutToBlow(a2);
//            c1.OnExploded(d);

//            Console.WriteLine("speedUp...");
//            for (int i = 0; i < 6;i++ )
//            {
//                c1.Accelerate(20);
//            }
//            c1.RemoveExploded(d);
//            Console.ReadLine();
//        }
//        public static void CarAboutTo(string msg)
//        {
//            Console.WriteLine(msg);
//        }
//        public static void CarIsAlmost(string msg)
//        {
//            Console.WriteLine("message from car: {0}",msg);
//        }
//        public static void CarExploded(string msg)
//        {
//            Console.WriteLine(msg);
//        }
//    }
//}

//namespace DelegateIsAll
//{
//    public delegate int BinOp(int x, int y);
//    public class SimpleMath
//    {
////      public static int Add(int x, int y)
//        public  int Add(int x, int y)
//        {
//            return x + y;
//        }
////      public static int Subtract(int x, int y)
//        public  int Subtract(int x, int y)
//        {
//            return x - y;
//        }
//    }
//    class Program
//    {
//        static void DisplayDelegateInfo(Delegate delObj)
//        {
//            foreach (Delegate d in delObj.GetInvocationList())
//            {
//                Console.WriteLine("Method name: {0}", d.Method);
//                Console.WriteLine("Type name: {0}", d.Target);
//            }
//        }
//        static void Main(string[] args)
//        {
////            BinOp b = new BinOp(SimpleMath.Add);
//            SimpleMath m = new SimpleMath();
//            BinOp b = new BinOp(m.Add);

//            DisplayDelegateInfo(b);
//            Console.WriteLine("10 + 10 is {0}", b(10, 10));
//            Console.ReadLine();
//        }
//    }
//}

 

//CarType.cs

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


namespace DelegateIsAll
{
    public class CarEventArgs:EventArgs
    {
        public readonly string msg;
        public CarEventArgs(string message)
        {
            msg = message;
        }
    }

    public class Car
    {
        public delegate void CarEventHandler(object sender, CarEventArgs e);
        public CarEventHandler NeedWash;
        public CarEventHandler NeedFix;

        private bool bNeedWash, bNeedFix;
        public bool dirty
        {
            get { return bNeedWash; }
            set { bNeedWash = value; }
        }
        public bool broken
        {
            get { return bNeedFix; }
            set { bNeedFix = value; }
        }
        public Car(bool bw, bool bf)
        {
            bNeedWash = bw;
            bNeedFix = bf;
        }
        public void checkCar()
        {
            if (bNeedWash)
                NeedWash(this, new CarEventArgs("please washing...."));
            else
                NeedWash(this, new CarEventArgs("already clean..."));
            if (bNeedFix)
                NeedFix(this, new CarEventArgs("please fixing..."));
            else
                NeedFix(this, new CarEventArgs("run good..."));
        }

    }
}
//namespace DelegateIsAll
//{
//    public class Car
//    {
//        public delegate void MaintainDelegate(Car c);
//        private string name;
//        private bool bNeedWash, bNeedFix;
//        public bool Dirty
//        {
//            get { return bNeedWash; }
//            set { bNeedWash = value; }
//        }
//        public bool Broken
//        {
//            get { return bNeedFix; }
//            set { bNeedFix = value; }
//        }
//        public Car()
//        {
//            name = "bird";
//        }
//        public Car(string pet, bool bW, bool bF)
//        {
//            name = pet;
//            bNeedWash = bW;
//            bNeedFix = bF;
//        }

//    }
//}

//namespace DelegateIsAll
//{
//    public class Car
//    {
//        public delegate void AboutToBlow(string msg);
//        public delegate void Exploded(string msg);

//        private AboutToBlow almostDeadList;
//        private Exploded explodedList;

//        public void OnAboutToBlow(AboutToBlow clientMethod)
//        { almostDeadList += clientMethod; }
//        public void OnExploded(Exploded clientMethod)
//        { explodedList += clientMethod; }

//        public void RemoveAboutToBlow(AboutToBlow clientMethod)
//        { almostDeadList -= clientMethod; }
//        public void RemoveExploded(Exploded clientMethod)
//        { explodedList -= clientMethod; }

//        public class Radio
//        {
//            public void TurnOn(bool on)
//            {
//                if (on)
//                    Console.WriteLine("Jamming...");
//                else
//                    Console.WriteLine("Quiet time...");
//            }
//        }
//        private int currSpeed;
//        private int maxSpeed;
//        private string petName;

//        bool carIsDead;
//        private Radio theMusicBox = new Radio();

//        public Car()
//        {
//            maxSpeed = 100;
//        }
//        public Car(string name, int max, int cur)
//        {
//            currSpeed = cur;
//            maxSpeed = max;
//            petName = name;
//        }
//        public void CrankTunes(bool state)
//        {
//            theMusicBox.TurnOn(state);
//        }

//        public void Accelerate(int delta)
//        {
//            if (carIsDead)
//            {
//                if (explodedList != null)
//                    explodedList("sorry, the car is dead...");
//            }
//            else
//            {
//                currSpeed += delta;

//                if (10 == maxSpeed - currSpeed
//                    && almostDeadList != null)
//                    almostDeadList("Careful ! gonna blow...");
//                if (currSpeed >= maxSpeed)
//                    carIsDead = true;
//                else
//                    Console.WriteLine("->CurSpeed = {0}", currSpeed);
//            }
//        }

//    }
//}

 

posted on 2009-06-02 12:25  Acor  阅读(270)  评论(0编辑  收藏  举报

导航