unity游戲開發-C#語言基礎篇(委托)

 class Program
    {
        private delegate void mydelegate(int a);//定義一個沒有返回值帶參數的委托類型
        delegate string mydelegate1();//定義一個有返回值類型的委托;

        delegate void mydelegate(int a);

        delegate string mydelegate1();
        static void Main(string[] args)
        {

             int a = 10; 定義一個int類型賦值為10;
            mydelegate my = new mydelegate(Test1);//實例化對象  委托實例化   一個實例化delegate類型(傳遞相應的方法)
            // mydelegate my = Test1;//直接使用類型 指向test的方法
             my(a);

            int a2 = 20;
            a2.ToString();
            mydelegate1 m1 = new mydelegate1(a2.ToString);
            Console.WriteLine("{0}",m1);

            Program p = new Program();

            Console.WriteLine(p);
            //Console.WriteLine("Hello World!");

            Action t = Test;//內置委托的類型  Action 是沒有返回值
            t();

            Action<int> t2 = Test1;//帶參使用

            t2(10);

            Func<int>  t5 = Test5;
            t5();

            Func<int,int > t6=Test6;
            t6(10);

            Func<int,int,double> t7=Test7;//參數1:穿入參數類型 參數2: 傳入參數類型 參數3:方法返回值類型
            t7(10,20);



            //練習

            mydelegate my = new mydelegate(Test111);//上面已經定義帶參數的委托類型 參數傳入的是帶參數的方法 注意方法返回值類型
            my(88);//委托后 調用方法; 傳入參數;


            mydelegate1 my1 = new mydelegate1(Test222);
            my1();


            Action t11 = Test11;//不帶參數
            //t11();




            Action<string> t22 = Test22;//帶參數

            // t22("123");



            Func<string> t33 = Test33;
            // t33();

            Func<string, string> t44 = Test44;

            // t44("888");




            Console.ReadKey();
        }
        public override string ToString()//重寫
        {
            return base.ToString();
        }


        static void Test()
        {
            Console.WriteLine("{Test");
        }

        static void Test1(int a)
        {
            Console.WriteLine("{0}", a);
        }

        static int Test5()
        {
            Console.WriteLine("沒有參數,但有返回值類型");
            return 10;
        }

        static int Test6(int a)
        {
            Console.WriteLine("{0}", a);
            return a;
        }

        static double Test7(int a, int b)
        {
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);
            return a;
        }




        static void Test11()
        {

            Console.WriteLine("沒有參數的委托");
        }


        static void Test22(string str)
        {

            Console.WriteLine("有參數的沒有返回值類型的委托:{0}", str);
        }


        static string Test33()
        {

            Console.WriteLine("沒有帶參有返回值類型的fun ");
            return "123!";
        }


        static string Test44(string str2)
        {

            Console.WriteLine("有返回值類型的帶參數的委托:{0}", str2);

            return "888";
        }



        public static void Test111(int a)
        {

            Console.WriteLine("{0}", a);
        }



        public static string Test222()
        {
            Console.WriteLine("有返回值類型的帶參數的委托方法{");
            return "1";
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容