JAVA通过反映方法实现C#的委托
来源:互联网 发布:使用u盘安装ubuntu 编辑:程序博客网 时间:2024/05/21 07:53
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方法时可能会产生大量的异常。
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对接
- Servlet3.1规范翻译——变更历史
- 一种系统异常设计思路
- EventThread线程对VSync的接收
- windows下搭建php环境
- XmlDocument和LINQ to XML两种方式对xml的创建、查询
- JAVA通过反映方法实现C#的委托
- Sencha Touch Carousel 去除底栏
- 用户体验超棒并且功能强大使用简单的javascript表单验证 - Parsley.js
- 分享30套精美的Web和手机开发UI素材包
- Servlet3.1规范翻译——词汇表
- Android解决获取控件Width和Height为0的问题
- 60款很酷的 jQuery 幻灯片演示和下载
- MFC中 修改list控件单元格的颜色
- Sencha Touch Carousel 自动切换