委托学习中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了委托学习中相关的知识,希望对你有一定的参考价值。

1.基本委托

技术分享
    class delete
    {
        delegate double MyDelegate(double message);
        public static double Ordinary(double price)
        {
            double price1 = 0.95 * price;
            Console.WriteLine("Ordinary Price : " + price1);
            return price1;
        }

        public static double Favourable(double price)
        {
            double price1 = 0.85 * price;
            Console.WriteLine("Favourable Price : " + price1);
            return price1;
        }

        static void Main(string[] args)
        {
            MyDelegate myDelegate = new MyDelegate(Ordinary);
            myDelegate += new MyDelegate(Favourable);
            Console.WriteLine("current price : " + myDelegate(100));
        }
    }
View Code

2反省委托

技术分享
   //public class lang
    //{
    //    public string name { set; get; }
    //}
    //public class c : lang {
    //    public string name { set; get; }
    //}
    //public delegate void Handler<T>(T obj);
    //public static void GetLang(lang l) {
    //    Console.WriteLine("this is lang"+l.name);
    //}
    //public static void getC(c cc){
    //    Console.WriteLine("this is    "+cc.name);
    //}
    //static void Main(string[] args) {
    //    Handler<lang> h = new Handler<lang>(GetLang);
    //    lang l = new lang();
    //    l.name = "test language";
    //    h(l);

    //    Handler<c> hc = new Handler<c>(getC);
    //    c cc = new c();
    //    cc.name = "c language";
    //    hc(cc);
    //}
View Code

3.观察者模式

技术分享
  //public delegate double Handler(double basicWages);

    //interface IWork {
    //     double GetWages(double basicWages);
    //}
    //public class Manager:IWork
    //{
    //    public double GetWages(double basicWages)
    //    {
    //        double totalWages = 1.5 * basicWages;
    //        Console.WriteLine("Manager‘s wages is : " + totalWages);
    //        return totalWages;
    //    }
    //}

    //public class Assistant:IWork
    //{
    //    public double GetWages(double basicWages)
    //    {
    //        double totalWages = 1.2 * basicWages;
    //        Console.WriteLine("Assistant‘s wages is : " + totalWages);
    //        return totalWages;
    //    }
    //}

    //public class WageManager
    //{
    //    private Handler wageHandler;

    //    //加入观察者
    //    public void Attach(Handler wageHandler1)
    //    {
    //        wageHandler += wageHandler1;
    //    }

    //    //删除观察者
    //    public void Detach(Handler wageHandler1)
    //    {
    //        wageHandler -= wageHandler1;
    //    }

    //    //通过GetInvodationList方法获取多路广播委托列表,如果观察者数量大于0即执行方法
    //    public void Execute(double basicWages)
    //    {
    //        if (wageHandler != null)
    //            if (wageHandler.GetInvocationList().Count() != 0)
    //                wageHandler(basicWages);
    //    }

    //    static void Main(string[] args)
    //    {
    //        WageManager wageManager = new WageManager();
    //        //加入Manager观察者
    //        Manager manager = new Manager();
    //        Handler managerHandler = new Handler(manager.GetWages);
    //        wageManager.Attach(managerHandler);

    //        //加入Assistant观察者
    //        Assistant assistant = new Assistant();
    //        Handler assistantHandler = new Handler(assistant.GetWages);
    //        wageManager.Attach(assistantHandler);

    //        //同时加入底薪3000元,分别进行计算
    //        wageManager.Execute(3000);
    //        Console.ReadKey();
    //    }
    //}
View Code

4.没弄懂

技术分享
    //public class WageManager
    //{
    //    IList<Worker> workerList = new List<Worker>();

    //    public void RegisterWorker(Worker worker)
    //    {
    //        workerList.Add(worker);
    //    }

    //    public void RemoveWorker(Worker worker)
    //    {
    //        workerList.Remove(worker);
    //    }
        
    //    public void Excute(double basicWages)
    //    {
    //        if (workerList.Count != 0)
    //            foreach (var worker in workerList)
    //                worker.GetWages(basicWages);
    //    }

    //    public delegate double Handel(double d);

    //    public void addHandel(Handel h) { 
    //        h += h;
    //    }

    //    static void Main(string[] args)
    //    {
    //        WageManager wageManager = new WageManager();
    //        //注册观察者
    //        wageManager.RegisterWorker(new Manager());
    //        wageManager.RegisterWorker(new Assistant());
    //        //同时输入底薪3000元,分别进行计算
    //        wageManager.Excute(5000);

    //        Console.ReadKey();
    //    }
    //}

    //public abstract class Worker
    //{
    //    public abstract double GetWages(double basicWages);
    //    public abstract double CheckWages(double basicWages);
    //}

    //public class Manager : Worker
    //{
    //    public override double CheckWages(double basicWages)
    //    {
    //        double totalWages = 1.5 * basicWages;
    //        Console.WriteLine("Manager‘s wages is " + totalWages);
    //        return totalWages;
    //    }
    //    //Manager实际工资为底薪1.5倍
    //    public override double GetWages(double basicWages)
    //    {
    //        double totalWages = 1.5 * basicWages;
    //        Console.WriteLine("Manager‘s wages is " + totalWages);
    //        return totalWages;
    //    }
    //}

    //public class Assistant : Worker
    //{
    //    public override double CheckWages(double basicWages)
    //    {
    //        double totalWages = 1.2 * basicWages;
    //        Console.WriteLine("Assistant‘s wages is " + totalWages);
    //        return totalWages;
    //    }
    //    //Assistant实际工资为底薪的1.2倍
    //    public override double GetWages(double basicWages)
    //    {
    //        double totalWages = 1.2 * basicWages;
    //        Console.WriteLine("Assistant‘s wages is " + totalWages);
    //        return totalWages;
    //    }
    //}
View Code

 

以上是关于委托学习中的主要内容,如果未能解决你的问题,请参考以下文章

最近项目中代码管理学习

最近项目中代码管理学习

基于深度学习的图像分类中代码的error

《深度学习入门 基于Python的理论与实现》书中代码笔记

java中代码执行顺序

Angular.js 学习二---$scope和$rootScope,Angular模块的run方法,依赖注入中代码压缩