委托学习小计2

昨天是演示了下简单的委托应用,还有一些比较比较稍微复杂一点委托应用或者委托协变~所谓委托协变,对于那些有着继承关系的类,为了避免在建立一个子类的委托类型用来只想返回该子类的方法,我们可以使用委托协变~ 协变允许方法具有的派生返回类型比委托中定义的更多。逆变允许方法具有的派生参数类型比委托类型中的更少-摘自MSDN

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

namespace DavidTest
{
    /// <summary>
    /// 汽车类
    /// </summary>
    public class Car
    {
        public delegate void CarMaintenanceDelegate(Car c);
        public delegate Car ObtainVehicalDelegate();

        #region 构造方法

        public Car()
        {
            this.Name = string.Empty;
            this.MaxSpeed = 0;
            this.CurrentSpeed = 0;
            this.IsDirty = false;
            this.ShouldRotate = false;
        }

        public Car(string name, int max, int current, bool washCar, bool rotateTires)
        {
            this.Name = name;
            this.MaxSpeed = max;
            this.CurrentSpeed = current;
            this.IsDirty = washCar;
            this.ShouldRotate = rotateTires;
        }

        #endregion

        public string Name { get; set; }

        public int MaxSpeed { get; set; }

        public int CurrentSpeed { get; set; }

        public bool IsDirty { get; set; }

        public bool ShouldRotate { get; set; }

    }

    /// <summary>
    /// 车库类
    /// </summary>
    public class Garage
    {
        private List<Car> theCars = new List<Car>();

        public Garage()
        {
            this.theCars.Add(new Car("Viper", 100, 0, true, false));
            this.theCars.Add(new Car("Fred", 150, 0, false, false));
            this.theCars.Add(new Car("Viper", 200, 0, false, true));
        }

        public void ProcessCars(Car.CarMaintenanceDelegate proc)
        {
            Console.WriteLine("*****Calling {0}******", proc.Method);

            if (proc.Target != null)
                Console.WriteLine("=> Target: {0}", proc.Target);
            else
                Console.WriteLine("=> Target is a static method}");

            foreach (Car car in theCars)
            {
                Console.WriteLine("\n=> Processing a Car");
                proc(car);
            }
        }
    }

    /// <summary>
    /// 服务部门类
    /// </summary>
    public class ServiceDepartment
    {
        
        public void WashCar(Car car)
        {
            if (car.IsDirty)
                Console.WriteLine("=> Cleaning a car");
            else
                Console.WriteLine("=> This car is already clean");
        }

        public void RotateTires(Car car)
        {
            if (car.ShouldRotate)
                Console.WriteLine("=> Tires have been rotated");
            else
                Console.WriteLine("=> Don't need to rotate");
        }
    }

    /// <summary>
    /// 竞技车类
    /// </summary>
    public class SportsCar : Car
    {
        public SportsCar() { }
    }

    public class DelegateVariance
    {
        public static Car GetBasicCar()
        { return new Car(); }

        public static SportsCar GetSportsCar()
        { return new SportsCar(); }
    }
}
Main函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DavidTest
{
    class Program
    {
        static void Main(string[] args)
        {
            
            #region 委托进阶测试

            Console.WriteLine("Advance Delete Test Begin:\n");

            Garage garage = new Garage();
            ServiceDepartment sd = new ServiceDepartment();

            garage.ProcessCars(new Car.CarMaintenanceDelegate(sd.WashCar));
            garage.ProcessCars(new Car.CarMaintenanceDelegate(sd.RotateTires));

            //委托协变
            Console.WriteLine("Delegate Convariance Begin:\n");
            Car.ObtainVehicalDelegate obvd = new Car.ObtainVehicalDelegate(DelegateVariance.GetBasicCar);
            Car c1 = obvd();
            Console.WriteLine("Obtained a {0}", c1);

            Car.ObtainVehicalDelegate obvdChild = new Car.ObtainVehicalDelegate(DelegateVariance.GetSportsCar);
            SportsCar c2 = obvdChild() as SportsCar;
            Console.WriteLine("Obtained a {0}", c2);

            #endregion


            Console.ReadLine();
        }
    }
}

学习小计暂且记录下~

posted @ 2013-02-10 01:59  红烧狮子头  阅读(373)  评论(0编辑  收藏  举报