Java vs. C# delegate
来源:互联网 发布:数据完整性稽核 caats 编辑:程序博客网 时间:2024/05/19 13:28
There is no delegate concept in JavaThe right-side C# program may be mimiced with reflection technology.====================================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) {} } }}
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;
0 0
- Java vs. C# delegate
- Java 仿C# delegate
- 看C# Delegate vs Event,Delegate和Control的Invoke和BeginInvoke 做笔记
- delegate 在Java和C#中的应用
- Java用反射实现C#的Delegate
- C# VS JAVA
- C# VS Java
- JavaSE:JAVA VS C#
- Java vs C#
- java vs c# struct
- Java vs. C#
- JAVA vs C#
- C# VS Java
- Java VS C#
- C# vs Java
- C# vs java
- C# Delegate
- C# delegate
- Linux 环境下如何使用NDK进行Android开发
- MATLAB引擎方式实现VC与MATLAB混合编程
- 面向对象编程已死
- C++模板函数——VC6下通过,在VS2010下报错 error C2668
- Nginx学习笔记(十八):事件处理框架
- Java vs. C# delegate
- Linux内核构建系统之三
- 第6周作业2-IF语句大显身手之成绩判断
- NSDictionary以及NSMutableDictionary用法总结
- matplot plot types 图表类型
- 机器的双网卡绑定 bonding
- NLS
- windows 机台信息
- 学习的步骤