zoukankan      html  css  js  c++  java
  • 委托利用GetInvocationList处理链式委托

    在利用委托进行函数代理的时候,我们习惯于用+=来把一个符合条件的委托加入委托链之中,如果加入了多个这样的函数,怎么一一对这些函数取返回值呢?请看下面的一个实例:

    View Code
    using System;
    
    namespace GetInvocationListDaemonCAPP
    {
        public delegate string TestDelegate();
    
        public class Program
        {
            static void Main(string[] args)
            {
                DelegateClass delegateClass = new DelegateClass();
    
                TestMethodOne one = new TestMethodOne();
                TestMethodTwo two = new TestMethodTwo();
                TestMethodThree three = new TestMethodThree();
                TestMethodFour four = new TestMethodFour();
    
                delegateClass.testDelegate += one.Say;
                delegateClass.testDelegate += two.Say;
                delegateClass.testDelegate += three.Say;
                delegateClass.testDelegate += four.Say;
    
                delegateClass.InvokeDelegate();
    
                Console.ReadKey();
            }
        }
    
        public class DelegateClass
        {
            public TestDelegate testDelegate;
    
            public void InvokeDelegate()
            {
                if (null != testDelegate)
                {
                    string resultStr = testDelegate();
                    Console.WriteLine(resultStr);
                }
            }
        }
    
        public class TestMethodOne
        {
            public string Say()
            {
                return "You called me from TestMethodOne~~~";
            }
        }
    
        public class TestMethodTwo
        {
            public string Say()
            {
                return "You called me from TestMethodTwo~~~";
            }
        }
    
        public class TestMethodThree
        {
            public string Say()
            {
                return "You called me from TestMethodThree~~~";
            }
        }
    
        public class TestMethodFour
        {
            public string Say()
            {
                return "You called me from TestMethodFour~~~";
            }
        }
    }

    在这个示例中,我用了一个委托代理了四个类型相同,返回值相同的函数,那么当我要获取这些函数的返回值的时候,会得到什么样的结果呢?

    You called me from TestMethodFour~~~

    结果就是上面的输出,原来,像这种方式的委托操作,会保留最后一个输出,前面几个都被OverWrite掉了。
    为了解决这个问题,GetInvocationList方法出现了。

    View Code
    using System;
    
    namespace GetInvocationListDaemonCAPP
    {
        public delegate string TestDelegate();
    
        public class Program
        {
            static void Main(string[] args)
            {
                DelegateClass delegateClass = new DelegateClass();
    
                TestMethodOne one = new TestMethodOne();
                TestMethodTwo two = new TestMethodTwo();
                TestMethodThree three = new TestMethodThree();
                TestMethodFour four = new TestMethodFour();
    
                delegateClass.testDelegate += one.Say;
                delegateClass.testDelegate += two.Say;
                delegateClass.testDelegate += three.Say;
                delegateClass.testDelegate += four.Say;
    
                delegateClass.InvokeDelegate();
    
                Console.ReadKey();
            }
        }
    
        public class DelegateClass
        {
            public TestDelegate testDelegate;
    
            public void InvokeDelegate()
            {
                if (null != testDelegate)
                {
                    //遍历委托链表
                    foreach (Delegate dele in testDelegate.GetInvocationList())
                    {
                        //类型转换
                        TestDelegate delegateClass = (TestDelegate)dele;
                        
                        //调用并 得到返回结果
                        string resultStr = delegateClass();
                        Console.WriteLine(resultStr);
                    }
                }
            }
        }
    
        public class TestMethodOne
        {
            public string Say()
            {
                return "You called me from TestMethodOne~~~";
            }
        }
    
        public class TestMethodTwo
        {
            public string Say()
            {
                return "You called me from TestMethodTwo~~~";
            }
        }
    
        public class TestMethodThree
        {
            public string Say()
            {
                return "You called me from TestMethodThree~~~";
            }
        }
    
        public class TestMethodFour
        {
            public string Say()
            {
                return "You called me from TestMethodFour~~~";
            }
        }
    }

    这样操作后,得到的运行结果如下:

    You called me from TestMethodOne~~~
    You called me from TestMethodTwo~~~
    You called me from TestMethodThree~~~
    You called me from TestMethodFour~~~
  • 相关阅读:
    程序的跨平台性:除了能够运行,还必须保证运行的结果。
    任务并发执行是一个宏观概念,微观上是串行的。
    Swing与AWT在事件模型处理上是一致的。
    我们在学习JDBC的时候会过度到J2EE。
    AWT是Java最早出现的图形界面,但很快就被Swing所取代。
    在生成一个窗体的时候,点击窗体的右上角关闭按钮激发窗体事件的方法:窗体Frame为事件源,WindowsListener接口调用Windowsclosing()。
    在java.util中有EventListener接口:所有事件监听者都要实现这个接口。
    事件模型指的是对象之间进行通信的设计模式。
    经验之谈:Swing的开发工作会非常的累,而且这项技术正在走向没落。避免从事有这种特征的工作。
    重点掌握集合的四种操作:增加、删除、遍历、排序。
  • 原文地址:https://www.cnblogs.com/scy251147/p/2783151.html
Copyright © 2011-2022 走看看