JAVA通过反映方法实现C#的委托
来源:互联网 发布:开淘宝店需要什么费用 编辑:程序博客网 时间:2024/06/14 08:43
C#委托实现了类内部的方法暴露给调用者,类似C++中的方法指针,方法以返回值的形式传递。这种设计方式可以实现统一方法名。
- import java.util.*;
- import java.lang.reflect.*;
- interface Command {
- Method executeMethod();
- }
- class StartComputer implements Command {
- Class classType = StartComputer.class;
- public Method executeMethod() {
- try {
- return classType.getMethod("innerMethod",new Class[]{ int.class });
- } catch(NoSuchMethodException e) {
- e.printStackTrace();
- return null;
- }
- }
- public void innerMethod(int param) {
- // TODO: something.
- System.out.println("----------------- OUTPUT ------------------");
- System.out.println(param);
- }
- }
- public class CommandStudy {
- public static Map<String, Class> commandMap =new HashMap<String, Class>();
- static {
- commandMap.put("StartComputer", StartComputer.class);
- }
- public static void main(String[] args) throws IllegalAccessException, InstantiationException , InvocationTargetException{
- Class startComputerClass = commandMap.get("StartComputer");
- Command someCommand = (Command)startComputerClass.newInstance();
- Method executeMethod = someCommand.executeMethod();
- executeMethod.invoke(someCommand, new Object[] {new Integer(200) });
- }
- }
import java.util.*;import java.lang.reflect.*;interface Command {Method executeMethod();}class StartComputer implements Command {Class classType = StartComputer.class;public Method executeMethod() {try {return classType.getMethod("innerMethod", new Class[]{ int.class });} catch(NoSuchMethodException e) {e.printStackTrace();return null;}}public void innerMethod(int param) {// TODO: something.System.out.println("----------------- OUTPUT ------------------");System.out.println(param);}}public class CommandStudy {public static Map<String, Class> commandMap = new HashMap<String, Class>();static {commandMap.put("StartComputer", StartComputer.class);}public static void main(String[] args) throws IllegalAccessException, InstantiationException , InvocationTargetException{Class startComputerClass = commandMap.get("StartComputer");Command someCommand = (Command)startComputerClass.newInstance();Method executeMethod = someCommand.executeMethod();executeMethod.invoke(someCommand, new Object[] { new Integer(200) });}}
Command类是公共接口类,对外开放的接口方法。StartCommand实现一个Command接口。在StartCommand的executeMethod方法的内部实现了方法的转移,把StartCommand里其它方法转移到接口方法executeMethod里,这样设计后,统一方法接口。
C#中委托对象delegate与JAVA中Method对象主要区别在于delegate里包含方法的对象,而Method主要是对方法的描述,不包含任务对象信息。
JAVA中用invoke方法时可能会产生大量的异常。
- JAVA通过反映方法实现C#的委托
- JAVA通过反映方法实现C#的委托
- C#委托的具体实现方法
- 最简单的C#委托实现方法
- Java 利用反射实现C#的委托
- C#通过委托实现窗体间的控制传递
- C#委托的实现
- C#源代码—声明委托,通过委托对象调用所指向的方法
- C#委托-委托作为方法的参数
- 对象间通信方法之一:通过委托实现(C#语言)
- C# 窗体传值的方法,通过委托来传值,代码相当简单。 - .NET技术 / C#
- C#方法的委托和java中的回调
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- C#委托的C++实现
- C# 委托的一个实现
- C#通过委托和异步调用实现窗体间的通讯
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- 以前的一些记录
- Activiti定时任务示例
- JNI之C++调用Java类 —— java.lang.String
- JSP乱码问题剖析与终极解决方案
- ZOJ 2176 Speed Limit
- JAVA通过反映方法实现C#的委托
- 分享70套约2000个视频教程(js,Jquery,java,.net,html5,php,数据库)
- highcharts中x轴曲线不连续时的解决办法
- java 倒序读取文件(最后一行开始读取)
- 线段树HDU1698(成段更新)
- mybatis 中IN 查询写法
- 在QQ通讯录之前拦截短信(android)
- hessian杂篇
- myeclipse10.0优化