关于 委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateDemo
{
    //声明委托
    public delegate void MyDel();
    //声明带参的委托
    public delegate void MyDel2(int num1, int num2);
    //声明带有返值的委托
    public delegate string MyDel3(string s);

    //声明委托用于演示匿名方法
    public delegate string ProcessString(string s);

    class Program
    {
        static void Main(string[] args)
        {

            TestDel t = new TestDel();

            #region 简单实例化委托与调用委托
            Console.WriteLine("-----以下是简单使用委托演示--------");
            //t.MyMethod();
            ///实例化委托,用一个方法来进行实例化
            ///该方法签名要与委托签名一致
            MyDel del = new MyDel(t.MyMethod);
            ///调用委托
            del();

            //C#2.0后可以这种方式实例化委托
            MyDel del4 = t.MyMethod;
            del4();

            //用静态方法进行实例化
            del4 = TestDel.MyStaticMethod;
            del4();

            //以下代码效果相同
            //MyDel2 del2 = new MyDel2(t.MyMethod);
            //del2(10, 20);
            MyDel2 del2 = t.MyMethod;
            del2(10, 20);

            //MyDel3 del3 = new MyDel3(t.MyMethod);
            //Console.WriteLine(del3("abc"));
            #endregion

            #region 匿名方法实例化委托
            Console.WriteLine("-----以下是匿名方法演示--------");
            //用匿名方法实例化委托
            ProcessString p = delegate(string inputString)
            {
                return inputString.ToUpper();
            };
            //通过委托调用匿名方法
            Console.WriteLine(p("aaaa"));
            #endregion

            #region 委托多播演示

            Console.WriteLine("-----以下是委托多播演示--------");
            MyDel mydel1 = t.MyMethod;
            MyDel mydel2 = t.MyMethod2;
            MyDel mydel3 = TestDel.MyMethod3;
            MyDel allMyDel = mydel1 + mydel2 + mydel3;
            allMyDel();
            allMyDel -= mydel3;
            allMyDel();

            #endregion

            #region 委托作为参数演示

            Console.WriteLine("-------以下是委托作为参数演示------");
            MyDel3 paramMyDel3 = t.MyMethod;
            TestDel.MyParamMethod("aaa", paramMyDel3);

            #endregion

            #region 委托作为返回值

            Console.WriteLine("---以下是委托作为返回值演示------");
            ///returnMyDel指向t.MyReturnMethod()的返回值
            MyDel3 returnMyDel = t.MyReturnMethod();
            ///returnMyDel指向t.MyMethod
            //MyDel3 returnMyDel = t.MyMethod;

            Console.WriteLine(returnMyDel("sssssssssssss"));
            #endregion

            #region 委托作为参数
            ////MyReturnDelegateTest my = new MyReturnDelegateTest();
            ////my.MyTest();
            //MyParamDelegateTest myParam = new MyParamDelegateTest();
            //myParam.AddBooks();
            //myParam.MyTest();
            //Console.ReadLine();
            #endregion
        }
    }
    public class TestDel
    {
        #region 普通方法
        public static void MyStaticMethod()
        {
            Console.WriteLine("My Static Method");
        }

        public void MyMethod()
        {
            Console.WriteLine("MyMethod");
        }
        public void MyMethod2()
        {
            Console.WriteLine("My Method 22222222222");
        }
        public static void MyMethod3()
        {
            Console.WriteLine("My Method 3333333333333");
        }

        public void MyMethod(int num1, int num2)
        {
            Console.WriteLine(num1 + num2);
        }
        public string MyMethod(string s)
        {
            return s.ToUpper();
        }
        #endregion

        /// <summary>
        /// 委托作为方法参数
        /// </summary>
        /// <param name="s"></param>
        /// <param name="del3"></param>
        public static void MyParamMethod(string s, MyDel3 del3)
        {
            Console.WriteLine(del3(s));
        }

        /// <summary>
        /// 委托作为返回值
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public MyDel3 MyReturnMethod()
        {
            ///返回符合委托规范的方法
            return MyMethod;
        }
    }

    #region 委托作为参数

    public class MyParamDelegateTest
    {
        BookDB bookDB = new BookDB();
        public void AddBooks()
        {
            bookDB.AddBook(new Book() { BookID = 1, BookName = "C#", Price = 123, IsPaperbook = true });
            bookDB.AddBook(new Book() { BookID = 1, BookName = "C#2", Price = 123, IsPaperbook = false });
            bookDB.AddBook(new Book() { BookID = 2, BookName = "ASP.Net", Price = 12, IsPaperbook = true });
            bookDB.AddBook(new Book() { BookID = 1, BookName = "ADO", Price = 23, IsPaperbook = false });
        }
        /// <summary>
        /// 用来实例化委托的函数
        /// </summary>
        /// <param name="b"></param>
        public void TestProcessBook(Book b)
        {
            if (b.IsPaperbook)
            {
                Console.WriteLine(b.BookName);
            }
            else
            {
                Console.WriteLine(b.BookName + "不是纸质的");
            }
        }
        /// <summary>
        /// 书的价格,全局
        /// </summary>
        double total = 0;
        /// <summary>
        /// 函数
        /// </summary>
        /// <param name="b"></param>
        public void TotalPrice(Book b)
        {
            total += b.Price;
        }
        public void MyTest()
        {

            //ProcessBook p=TestProcessBook;
            //ProcessBook p1=TotalPrice;
            //ProcessBook p2=p+p1;
            //把方法名做为参数进行传递
            bookDB.PrintBook(TestProcessBook);
            bookDB.PrintBook(TotalPrice);
            Console.WriteLine(total);
        }
    }
    /// <summary>
    /// 委托
    /// </summary>
    /// <param name="b"></param>
    public delegate void ProcessBook(Book b);
    /// <summary>
    /// 书的函数
    /// </summary>
    public class BookDB
    {
        public List<Book> books = new List<Book>();
        public void AddBook(Book b)
        {
            books.Add(b);
        }
        public void PrintBook(ProcessBook process)
        {
            foreach (var book in books)
            {
                process(book);
            }
        }
    }
    /// <summary>
    /// 书类
    /// </summary>
    public class Book
    {
        public int BookID { get; set; }
        public string BookName { get; set; }
        public double Price { get; set; }
        public bool IsPaperbook { get; set; }
    }


    #endregion

    #region  委托作为返回值

    public delegate int MyReturnDelegate(int num1, int num2);

    public class MyReturnDelegateTest
    {
        public void MyTest()
        {
            MyCalcuate myCalcuate = new MyCalcuate();
            do
            {
                Console.WriteLine("请输入符号进行以计算( + - * /)");
                string oper = Console.ReadLine();
                Console.WriteLine("请输入操作数1");
                string num1 = Console.ReadLine();

                Console.WriteLine("请输入操作数2");
                string num2 = Console.ReadLine();

                MyReturnDelegate myReturn = myCalcuate.Calcuate(oper);
                int result = myReturn(int.Parse(num1), int.Parse(num2));
                Console.WriteLine(
                    string.Format("{0}{1}{2}={3}", num1, oper, num2, result));

                Console.WriteLine("您还要继续吗?Y/N");
                //string continueFlag = Console.ReadLine();
                //if (continueFlag.ToUpper() == "N") break;

            } while (Console.ReadLine().ToUpper() != "N");
        }
    }

    public class MyCalcuate
    {
        public MyReturnDelegate Calcuate(string oper)
        {
            MyReturnDelegate myReturn = null;
            switch (oper)
            {
                case "+":
                    myReturn = delegate(int num1, int num2) { return num1 + num2; };
                    break;
                case "-":
                    myReturn = delegate(int num1, int num2) { return num1 - num2; };
                    break;
                case "*":
                    myReturn = delegate(int num1, int num2) { return num1 * num2; };
                    break;
                case "/":
                    myReturn = delegate(int num1, int num2) { return num1 / num2; };
                    break;
                default:
                    break;
            }
            return myReturn;
        }
    }
    #endregion

}
posted @ 2012-10-24 14:43  南潇湘  阅读(162)  评论(0编辑  收藏  举报