示例#1
0
        public static void Test4()//引用类型:接口、委托、事件
        {
            //=========================接口
            CDPlayer player1 = new CDPlayer();

            player1.NextTrack();
            player1.Open();
            player1.CurrentTrack();
            player1.NextTrack();
            player1.NextTrack();
            player1.CurrentTrack();
            player1.PreviousTrack();
            player1.CurrentTrack();
            player1.Close();
            Console.WriteLine();
            //=========================显式接口
            Computer cpt = new Computer();
            ISystem  sys = cpt;

            sys.Run();
            sys.Calculate();
            ICalculator cal = cpt;

            cal.Calculate();
            Console.WriteLine();
            //=========================委托
            Say say1 = ASay;
            Say say2 = BSay;
            Say say3 = say1 + say2;

            say1("Hi");
            say2("Hi");
            say3("Hello");
            Console.WriteLine();
            //=========================事件
            Button button1 = new Button("Button1");
            Button button2 = new Button("Button2");

            button1.Click += Button1_Click; //订阅
            button2.Click += Button1_Click; //订阅
            //event1.myEvent += new MyEvent1.Handler(PrintParam);//订阅
            button1.OnClick();
            button2.OnClick();
            //总过程:订阅,触发。事件调用委托,委托调用方法(方法参数是定义好的对象)。
            button1.Click -= Button1_Click; //卸载订阅
            button1.OnClick();              //已无效果
        }