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



















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