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

最新文章

  1. 51nod 1228 序列求和 ( 1^k+2^k+3^k+...+n^k )
  2. day 2远程连接Linux系统管理
  3. PHP流程控制(二)
  4. 忘记 mysql5.5.24 数据库 root 密码
  5. vs2010根据字符串内容添加断点
  6. UML相关工具一览
  7. MySQL的安装(比较详细的安装步骤,包括客户端和服务端的安装,还有环境变量的配置以及使用Windows service启动MySQL)
  8. BZOJ_2661_[BeiJing wc2012]连连看_费用流
  9. Scrapy 框架 中间件,信号,定制命令
  10. 10.scrapy框架简介和基础应用
  11. sqlserver 数据库插入汉字变成乱码的解决方案
  12. mysql查询高级用法
  13. Executor 框架详解
  14. 尚硅谷STRUTS2视频学习笔记
  15. 05 Maven 生命周期和插件
  16. 关于OI中简单的常数优化
  17. 分布式网上商城项目-solr搜索功能错误
  18. Emmet 也有快速生成文件头的功能
  19. javascript语言历史
  20. [bzoj] 2716 天使玩偶 || CDQ分治

热门文章

  1. python 之路,Day27 - 主机管理+堡垒机系统开发
  2. 关于Linux下面msyql安装后并未设置初始密码,但是登录报错“Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: NO)”的解决方案
  3. 注册表修改IP地址和DNS等信息
  4. Listbox与dataGridView的获取鼠标点击事件的区别!!!
  5. JavaScript 之 关键内容
  6. java 每天一练(二)
  7. 学习C++——只声明忘记定义了
  8. 使用wireshark抓本机之间的包(转)
  9. Tomcat部署多个项目及相关配置
  10. a便签 rel属性改变链接打开页面的方式