打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
委托学习小计2

委托学习小计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(); }    }}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#多线程简单例子
C# 第一天代码
.NET 委托类型解析(下)
C# 数组的交集、差集、并集
C#之time
上课笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服