首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

delegate 在Java跟C#中的应用

2014-01-15 
delegate 在Java和C#中的应用Delegate是引用类型允许间接访问方法。以下是简单和多维的委托DelegateDelegat

delegate 在Java和C#中的应用

Delegate是引用类型允许间接访问方法。以下是简单和多维的委托Delegate
Delegates are reference types which allow indirect calls to methods. There are single and multicastdelegates.============================================ using System; using System.IO; public class DelegateTest {     public delegate void Print (String s);     public static void Main()     {         Print s = new Print (toConsole);         Print v = new Print (toFile);         Display (s);         Display (v);     }     public static void toConsole (String str)     {         Console.WriteLine(str);     }     public static void toFile (String s)     {         File f = new File("delegate.txt");         StreamWriter fileOut = f.CreateText();         fileOut.WriteLine(s);         fileOut.Flush();         fileOut.Close();     }     public static void Display(Print pMethod)     {         pMethod("This should be displayed in the console");     } }
 A delegate instance encapsulates one or more methods,  each of which is referred to as a callable entity.  To add or reduce a list of calls by using operators += or -=. for example Print p = s + v; s += v;
对比可以得出的概念是:Java中没有这个Delegate的概念,但是通过反射的方法可以实现,C#却可以通过直接使用Delegate用来实现函数方法的间接调用。

转载自:http://blog.csdn.net/zzhays/article/details/11518925

热点排行