事件,委託 執行個體

來源:互聯網
上載者:User

委託
使用委託的步驟:
1.定義委託
2.執行個體化委託
3.使用委託

[存取修飾詞] delegate 傳回型別 委託名();

    class Program
    {
            //定義委託Call
            public delegate int Call(int num1,int num2);

        class Math
        {
            public int Multiply(int num1, int num2)
            {
                               //語句;
            }
            public int Divide(int num1, int num2)
            {
                               //語句;
            }
        }
        class TestDelegates
        {
            static void Main()
            {
                Call objCall;
                Math objMath = new Math();
                //執行個體化委託Call
                objCall = new Call(objMath.Multiply);
            }
        }
    }

調用委託
調用委託意味著使用委託對方法進行執行個體化.

    class Delegates
    {
        //委託定義
        public delegate int Call(int num1,int num2);
        class Math
        {
            //乘法方法
            public int Multiply(int num1,int num2)
            {
                return num1*num2;
            }
            //除法方法
            public int Divide(int num1, int num2)
            {
                return num1 / num2;
            }
        }
        class TestDelegates
        {
            static void Main()
            {
                //儲存結果的變數
                int result;
                //委託的對象
                Call objCall;
                //Math類的對象
                Math objMath = new Math();
                //執行個體化委託
                objCall = new Call(objMath.Multiply);
                //調用委託
                result = objCall(5, 3);
                Console.WriteLine(result);
            }
        }
    }

控制台輸出:
15

定義事件
[存取修飾詞] event 委託名 事件名;

定義事件時,發行者首先定義委託,然後根據該委託定義事件.
//定義一個委託
public delegate void delegateMe();
//定義一個事件
private event delegateMe eventMe;
為什麼要用委託來定義一個事件?因為這裡用委託限定了事件引發函數的類型,即函數的參數個數,類型以及函數的傳回值類型.

訂閱事件只是添加了一個委託,事件引發時該委託將調用一個方法.
//objA方法訂閱了事件eventMe
eventMe += new delegateMe(objA.Method);

引發事件
對特定條件進行檢查,如果指定的條件為真,則引發事件eventMe
if(true)
{
    //引發eventMe事件
    eventMe();
}

綜合例子:

 class Student
    {
        //定義委託
        public delegate void DelegateRegisterFinish();
        //通過委託定義事件
        public event DelegateRegisterFinish RegisterFinish;
        private string _name;
        public Student(string name)
        {
            _name = name;
        }
        public void Register()
        {
            Console.WriteLine("學生{0}進行註冊",_name);
            if (RegisterFinish != null)
            {
                //引發事件
                RegisterFinish();
            }
        }
    }
    class Registe
    {
        static void Main()
        {
            Console.WriteLine("輸入註冊學生的姓名:");
            string studentName = Console.ReadLine();
            Student student = new Student(studentName);        
            //訂閱Student類的RegisterFinish事件
            student.RegisterFinish += new Student.DelegateRegisterFinish(student_Regis);
            student.Register();
        }
        private static void student_Regis()
        {
            Console.WriteLine("註冊成功");
        }
    }

控制台輸出:
輸入註冊學生的姓名:
Tom
學生Tom進行註冊
註冊成功

練習:

建立事件ZeroEntered,接受使用者輸入的兩個數.如果使用者輸入數字0,則應該引發該事件並調用方法Disp來

顯示"不允許以零為除數",如果輸入數字大於0,則對數字進行除法計算並顯示結果.

    class Test
    {
        //定義一個委託
        public delegate void delegateMe();
        //定義一個事件
        public event delegateMe ZeroEntered;
        public void Register(int num1,int num2)
        {
            if (num2 == 0)
            {
                //引發事件
                ZeroEntered();
            }
            else
            {
                Console.WriteLine(num1/num2);
            }

        }

    }
    class Tests
    {
        static void Main()
        {
            Console.WriteLine("輸入被除數");
            int num = int.Parse(Console.ReadLine());
             Console.WriteLine("輸入除數");
            int num2 = int.Parse(Console.ReadLine());
            Test obj = new Test();        
            obj.ZeroEntered+=new Test.delegateMe(Disp);
            obj.Register(num, num2);
        }
        public static void Disp()
        {
            Console.WriteLine("不允許以零為除數");
        }
    }

第一次
控制台輸出:
輸入被除數
10
輸入除數
5
2

第二次
控制台輸出:
輸入被除數
10
輸入除數
0
不允許以零為除數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.