A11_C#封裝的委託

/*
 *  Action 委託 沒有返回值沒有參數
 * 
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A11_DelegateInSystem
{
    class Demo1
    {

        Action actHandler; //聲明Action委託

        public Demo1()
        {
            actHandler += Test1;
            actHandler += Test2;
        }

        public void Test1()
        {
            Console.WriteLine("Message 1");
        }

        public void Test2()
        {
            Console.WriteLine("Message 2");
        }

        //調用委託
        public void DisplayInfo()
        {
            actHandler();
        }

        static void Main1(string[] args)
        {

            Demo1 obj = new Demo1();

            obj.DisplayInfo();
        }
    }
}

/*
 *  泛型Action委託 有參數的委託
 *  1.Action<T>
 *  2.泛型委託的參數可以定義參數的個數,最多16個
 *  
 *  
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A11_DelegateInSystem
{

    class Demo2
    {
        //聲明泛型委託
        Action<string> actHandle;

        public Demo2()
        {
            //註冊泛型委託
            actHandle += Test1;
            actHandle += Test2;
        }

        public void Test1(string str)
        {
            Console.WriteLine("Test1 參數 = " + str);
        }

        public void Test2(string str)
        {
            Console.WriteLine("Test2 參數 = " + str);
        }

        public void DisplayInfo()
        {
            actHandle("Test Message Args");
        }

        static void Main1(string[] args)
        {
            Demo2 obj = new Demo2();
            obj.DisplayInfo();
        }
    }
}

/*
 * Func委託
 * 1.必須有一個返回值
 * 2.尖括號中最後一個參數爲返回類型
 * 3.如果尖括號中只有一個類型,那麼該類型爲返回值類型
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A11_DelegateInSystem
{
    class Demo3
    {
        Func<string> funcHandler; //無參
        Func<string, int> funcHandlerWithParam; //有一個參數

        public Demo3()
        {

            funcHandler += InvkeMethod;

            funcHandlerWithParam += InvokeMethodWithParam;
        }

        public string InvkeMethod()
        {
            Console.WriteLine("無參方法,返回類型爲string");
            return "Message";
        }

        public int InvokeMethodWithParam(string str)
        {
            Console.WriteLine("有參數方法,返回值類型爲int");
            return 0;
        }

        public void DisplayInfo()
        {
            //調用無參數委託
            funcHandler();
            //調用有參數委託
            funcHandlerWithParam("Message");
        }

        static void Main1(string[] args)
        {
            Demo3 obj = new Demo3();
            obj.DisplayInfo();
        }
    }
}

/*
 * Predicate委託
 * 只能接受一個參數
 * 必須返回bool類型
 * 等價於Func<T,bool>
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A11_DelegateInSystem
{
    class Demo4
    {
        Predicate<string> preHandler;
        public Demo4()
        {
            preHandler += PredicateInvokeMethod;
        }

        private bool PredicateInvokeMethod(string str)
        {
            Console.WriteLine("返回bool值的,有一個參數的方法,參數 str = {0}", str);
            return true;
        }

        private void DisplayInfo()
        {
            preHandler("str Arg");
        }

        static void Main1(string[] args)
        {
            Demo4 obj = new Demo4();
            obj.DisplayInfo();
        }
    }
}


/*
 * 委託的優點
 * 減少調用方和被調用方之間的偶合性
 * 案例:取1-1000之間的質數
 * **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A11_DelegateInSystem
{
    class Demo5
    {
        //求給定的一個數字,判斷是否爲質數
        private bool JudgeSS(int num)
        {
            bool boolResult = true;
            for (int i = 2; i < num -1; i++)
            {
                if (num % i == 0)
                {
                    boolResult = false;
                    break;
                }
            }

            return boolResult;
        }

        public void DisplayAllNumber()
        {
            for (int i = 2; i<1000; i++)
            {
                if (JudgeSS(i)) //普通調用
                {
                    Console.WriteLine(i);
                }
            }

        }

        public void DisplayAllNumber(Func<int,bool> funHandler)
        {
            for (int i = 2; i < 1000; i++)
            {
                if (funHandler(i)) //委託調用
                {
                    Console.WriteLine(i);
                }
            }

        }

        public void DisplayInfo()
        {
            DisplayAllNumber(JudgeSS);
        }

        static void Main(string[] args)
        {
            Demo5 obj = new Demo5();
            obj.DisplayInfo();
        }
    }
}



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章