There is no delegate concept in Java
The right-side C# program may be mimiced
with reflection technology.
在Java中没有delegate的概念,而C#中delegate使用的是类似Java中反射的工具。
import java.lang.reflect.*;
import java.io.*;
public class Test
{
public static void main(String[] args) throws Exception
{
String[] list= {"to","be","or","not","to","be"};
Method m1 = Test.class.getMethod("toConsole",new Class[] {String.class});
Display(m1, list);
Method m2 = Test.class.getMethod("toFile",new Class[] {String.class});
Display (m2, list);
}
public static void toConsole (String str)
{
System.out.print(str+" ");
}
public static void toFile (String s)
{
File f = new File("delegate.txt");
try{
PrintWriter fileOut =
new PrintWriter(new FileOutputStream(f));
fileOut.write(s);
fileOut.flush();
fileOut.close();
}catch(IOException ioe) {}
}
public static void display(Method m, String[] list)
{
for(int k = 0; k < list.length; k++) {
try {
Object[] args = {new String(list[k])};
m.invoke(null, args);
}catch(Exception e) {}
}
}
}
Delegate是引用类型允许间接访问方法。以下是简单和多维的委托Delegate
Delegates are reference types which allow
indirect calls to methods. There are single and multicast
delegates.
============================================
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
分享到:
相关推荐
C# Delegate讲解C# Delegate讲解C# Delegate讲解C# Delegate讲解
C#中的事件和委托(Delegate,Event)
本文实例讲述了C#使用委托(delegate)实现在两个form之间传递数据的方法。分享给大家供大家参考。具体分析如下: 关于Delegate【代理、委托】是C#中一个非常重要的概念,向前可以推演到C++的指针,向后可以延续到匿名...
Context Delegate Source C# - C# Source code
适合初学者了解C#的delegate,是一个非常简单的例子。
C# delegate thread范例 若不懂在代码中,线程如何调用的可以看看
CSharp_Delegate C#委托 本人博客中的示例代码
最近收集的VB.Net-C#多线程Thread-代理委托delegate编程。文章列表: c#.net多线程同步.txt C#WebBrowser页面与WinForm交互技巧一.txt C#多线程编程-多细全.txt C#多线程编程简单实例.txt C#多线程窗体控件安全访问....
委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易。它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每次见到委托和事件就...
c#的回调函数(delegate关键字)
c#中关于委托delegate的经典的实例,可以使我们清楚的了解delegate的使用方法及含义。
delegate与事件的实例,很简单的例子,帮助初学者理解,其中有两个delegate,一个有参数,一个无参数
网上有很多关于C++ delegate机制的文章,但都讲的是函数指针的内容,上周就C++中实现C#的delegate机制的问题研究了好几天,查看了很多相关资料,才解决了问题,现将我写的C++ delegate测试程序贴出来,希望能帮到有...
C#中有很多易混淆的关键词,例如delegate,Func, Action和 Predicate。Func, Action和 Predicate本质上都是delegate,下面看一下delegate概念。 1 delegate概念 delegate本质上就是一个指向函数的指针,可以指向...
请说明在C#中命名空间的作用和用法。 4. C#语言中的面向对象编程(Object-Oriented Programming)特性有哪些?请说明在C#中的面向对象编程思想。 5. 在C#中如何进行异常处理(Exception Handling)?请说明在C#中的...
在qt的MVC模式中,运用控件在delegate中显示的方法。
该文档给出了在不同线程间实时更新控件textbox数据的代码,利用了委托(delegate)
委托回调
从序言中,大家应该对委托和事件的重要性有点了解了吧,虽然说我们现在还是能模糊,但是从我的大白话系列中,我会把这些概念说的通俗易懂的。首先,我们还是先说说委托吧,从字面上理解,只要是中国人应该都知道这个...
本ppt结合场景和实例对C#委托(delegate)的基础进行了讲解:包括了委托的定义,基本语法,同步异步调用,匿名委托的使用,泛型委托,lamda表达式,剖析了委托协变、委托逆变。对委托在.net 3.5框架中的身影进行了分析...