zoukankan      html  css  js  c++  java
  • 委托学习小记(2)

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

    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(); }
        }
    }
    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();
            }
        }
    }

    --------------------------------------

    欢迎您,进入 我系程序猿 的cnBlog博客。

    你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。

    You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.

    --------------------------------------

    分享到QQ空间  

  • 相关阅读:
    Zend_Controller架构
    PHP构造函数的执行顺序
    MySQL性能优化的最佳21条经验
    MySQL触发器学习总结
    使用Zend_Auth和Zend_Acl进行登录认证及根据用户角色进行权限控制
    手动释放你的资源(Please release resources manually)
    InfoPath/SharePoint/WebParts项目组章程 无为而为
    解决错误:sql_variant is incompatible with xml (ASP.NET 2.0 / XML数据类型 ) 无为而为
    使用ISA2004发布SharePoint网站到外部网,需要使用链接转换 无为而为
    InfoPath/SharePoint/WebParts项目组 下一步的工作和团队未来的规划给队员的公开信 无为而为
  • 原文地址:https://www.cnblogs.com/jqmtony/p/2910909.html
Copyright © 2011-2022 走看看