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用来实现函数方法的间接调用。

Java与.net的区别delegate和event的更多相关文章

  1. 浅谈c#中的delegate和event了

    一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那就趁着阳光明媚的早晨简单来谈谈delegate和ev ...

  2. 第一章、C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    第一章.C#委托和事件(Delegate.Event.EventHandler.EventArgs) 分类: 学习笔记-C#网络编程2012-12-08 14:10 7417人阅读 评论(3) 收藏  ...

  3. delegate和event

    经过查阅资料和自己的理解整理出来的,欢迎大家指教. delegate和event 何时使用: 异步的时候,比如加载完成通知. 需要回调的时候,比如按钮点击.动画播放结束等. 发送事件通知的时候. 比如 ...

  4. [转] C#中的delegate 和 event

    转至:here 终于会用c#中的delegate(委托) 作者:qq826364410 引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delega ...

  5. java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别

    java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别   以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自 ...

  6. java 和javaw 的区别——<转>

    java 和javaw 的区别 javaw.exe用法和java.exe 相同 javaw的程序不在java console 上面显示任何东西,如果在开发程序,就用java,这样可以看到错误提示, 如 ...

  7. .NET 中易混淆的概念(Delegate vs Event)

    事件(event)是一个非常重要的概念,我们的程序时刻都在触发和接收着各种事件:鼠标点击事件,键盘事件,以及处理操作系统的各种事件.所谓事件就是 由某个对象发出的消息.比如用户按下了某个按钮,某个文件 ...

  8. java与javac的区别

    1.前提:java分为两部分 一个是编译(javac命令),一个是运行(java命令) 2.java与javac的区别 javac负责的是编译,将.java文件编译成.class文件,当执行javac ...

  9. Java中equal和==区别及String创建过程

    Java中equal和==区别 1.起因 在一段Java代码中,使用了两种实现方式. //第一种命令行输入 int main (String[] args) { if(args[0] == " ...

随机推荐

  1. winapi获取鼠标位置

    using System; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; na ...

  2. readonly 与 const

    readonly MSDN定义:readonly 关键字是可以在字段上使用的修饰符.当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数 ...

  3. .Net程序员 Solr-5.3之旅 (一)Solr入门

    阅读目录 引言 Lunece是什么? Solr是什么 JAVA环境搭建 JAVA环境搭建之变量配置 Tomcat简单配置 结尾 引言 君子生非异也,善假于物也. Java和.Net哪个好,我们也不需要 ...

  4. MVC部署出现HTTP 404 错误

    asp.net mvc部署出现问题,http错误404.0,报错如下图: 在网上找了好多方法都不行.最后我的解决方案是: 打好这个补丁就行了http://support.microsoft.com/k ...

  5. C#中创建线程,创建带参数的线程

    线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函 构造函数定义: 无参数委托 [SecuritySafeC ...

  6. (转)php5中类的学习

    类的结构: 类的内部能可能有三种东西,就是常量(constant),属性(property)和方法(method),功能可以理解成类外部的常量,变量和函数.     复制代码代码如下: <?ph ...

  7. 全文索引--自定义chinese_lexer词典

    全文索引它的数据字典本来就是自己加密过的数据格式,只有翻译过来了,才可以修改.这样修改后再生成它自己的数据格式文件,覆盖掉原来的,就会将新添加的关键词加入进去了!! 以下操作是在Oracle服务器安装 ...

  8. iOS几种简单有效的数组排序方法

    第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 NSComparator cmptr = ^(id obj1 ...

  9. 防止sql注入 参数化解决方案

    StringBuilder strSql=new StringBuilder(); strSql.Append("insert into T_SysLog("); strSql.A ...

  10. underscorejs-every学习

    2.10 every 2.10.1 语法: _.every(list, predicate, [context]) 2.10.2 说明: 对list集合的每个成员根据predicate进行真值检测,如 ...