基本类型--委托(一)

 151初识委托

 //声明一个委托,它的实例引用一个方法,该方法接受一个Int32的参数并返回void
    internal delegate void Feedback(Int32 value);
    
public sealed class Program {
        
public static void Main() {
            StaticDelegateDemo();
            InstanceDelegateDemo();
            ChainDelegateDemo1(
new Program());
            ChainDelegateDemo2(
new Program());
        }

        
//委托调用静态回调方法
        private static void StaticDelegateDemo() {
            Console.WriteLine(
"---StaticDelegateDemo---");
            Counter(
13null);
            Counter(
13new Feedback(FeedBackToConsole));
            Counter(
13new Feedback(FeedBackToMsgBox));
            Console.ReadLine();
        }

        
//委托调用实例回调方法
        private static void InstanceDelegateDemo() {
            Console.WriteLine(
"---InstanceDelegateDemo---");
            Program p 
= new Program();
            Counter(
13new Feedback(p.FeedBackToFile));
            Console.ReadLine();
        }

        
//顺序调用多个静态、实例方法
        private static void ChainDelegateDemo1(Program p) {
            Console.WriteLine(
"---ChainDelegateDemo1---");
            Feedback fb1 
= new Feedback(FeedBackToConsole);
            Feedback fb2 
= new Feedback(FeedBackToMsgBox);
            Feedback fb3 
= new Feedback(p.FeedBackToFile);

            Feedback fbChain 
= null;
            fbChain 
= (Feedback)Delegate.Combine(fbChain, fb1);
            fbChain 
= (Feedback)Delegate.Combine(fbChain, fb2);
            fbChain 
= (Feedback)Delegate.Combine(fbChain, fb3);
            Counter(
45, fbChain);

            Console.WriteLine();
            fbChain 
= (Feedback)Delegate.Remove(fbChain, new Feedback(FeedBackToMsgBox));
            Counter(
67, fbChain);
            Console.ReadLine();
        }

        
//顺序调用多个静态、实例方法
        private static void ChainDelegateDemo2(Program p) {
            Console.WriteLine(
"---ChainDelegateDemo2---");
            Feedback fb1 
= new Feedback(FeedBackToConsole);
            Feedback fb2 
= new Feedback(FeedBackToMsgBox);
            Feedback fb3 
= new Feedback(p.FeedBackToFile);

            Feedback fbChain 
= null;
            fbChain 
+= fb1;
            fbChain 
+= fb2;
            fbChain 
+= fb3;
            Counter(
89, fbChain);

            Console.WriteLine();
            fbChain 
-= new Feedback(FeedBackToMsgBox);
            Counter(
1011, fbChain);
            Console.ReadLine();
        }

        
//
        private static void Counter(Int32 from, Int32 to, Feedback fb) {
            
for (int i = from; i <= to; i++){
                
if (fb != null)
                    fb(i);
            }

        }

        
private static void FeedBackToConsole(Int32 value) {
            Console.WriteLine(
"控制台输出:" + value);
        }

        
private static void FeedBackToMsgBox(Int32 value) {
            Console.WriteLine(
"消息框显示:" + value);
        }

        
private void FeedBackToFile(Int32 value) {
            StreamWriter sw 
= new StreamWriter("C:/FeedBackToFile.txt"true);
            sw.WriteLine(
"文件显示:" + value);
            sw.Close();
        }

    }

152使用委托来回调静态方法

153使用委托回调实例方法

154委托揭秘

155使用委托回调多个方法(链式)
posted @ 2008-05-27 09:44  青羽  阅读(646)  评论(0编辑  收藏  举报