C#委託本質探索 七、方法變數作為函數參數傳遞

來源:互聯網
上載者:User

// 將方法變數作為函數參數傳遞,又叫回調
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1.SeventhTest
{
    class SeventhTest
    {
        public void DoTest()
        {
            領導 朱哥 = new 領導();
            下屬 小薇 = new 下屬();

            朱哥.增加任務(小薇.自己的工作, "運營魔獸世界");
            朱哥.處理任務();

            Console.ReadLine();
            朱哥.增加任務(小薇.自己的工作, "處理業內競爭");
            朱哥.處理任務();
        }

    }

    delegate string 委託任務(string 任務內容);

    class 領導
    {
        private Hashtable 工作清單;

        public void 增加任務(委託任務 一個任務, string 任務類型)
        {
            if(工作清單 == null)
                工作清單 = new Hashtable();
            工作清單[任務類型] = 一個任務;
        }

        public void 處理任務()
        {
            foreach(DictionaryEntry 任務項 in 工作清單)
            {
                string 結果 = ((委託任務)任務項.Value)((string)任務項.Key);
                Console.WriteLine(結果);
            }
        }

    }

    class 下屬
    {
        public string 自己的工作(string 任務內容)
        {
            StringBuilder 工作內容 = new StringBuilder();
            if (任務內容 == "運營魔獸世界")
            {
                工作內容.AppendLine("安排裝置採購");
                工作內容.AppendLine("招募客服,上崗培訓");
                工作內容.AppendLine("廣告宣傳");
                工作內容.AppendLine("遊戲上市");
                工作內容.AppendLine("推出活動");
                工作內容.AppendLine("…………");
            }
            else if (任務內容 == "處理業內競爭")
            {
                工作內容.AppendLine("調查競爭者");
                工作內容.AppendLine("展開鬥爭");
            }
            return 工作內容.ToString();
        }

    }

    // 上一例,領導將自己做的事情向外公開,下屬對領導公開的事情做進一步處理.
    // 這個例子,領導接收外部的事情,並處理.
    // 一個是向外開放,一個是向內接收.
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.