JAVA通过反映方法实现C#的委托
2013-02-10 19:06
876 查看
C#委托实现了类内部的方法暴露给调用者,类似C++中的方法指针,方法以返回值的形式传递。这种设计方式可以实现统一方法名。
Java代码
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代码
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#的委托
- 详解C#中通过委托来实现回调函数功能的方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- 对象间通信方法之一:通过委托实现(C#语言)
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- JAVA实现图像缩放(通过 java.awt.geom的仿射变换结合java.awt.image的各种插值方法实现)
- Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针
- Java通过反射实现方法调用
- Java通过调用C/C++实现的DLL动态库――JNI的方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#实现通过ffmpeg从flv视频文件中截图的方法
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- JAVA实现图像缩放(通过 java.awt.geom的仿射变换结合java.awt.image的各种插值方法实现)
- Java中没有C#的out关键字,但可以通过数组实现类似的效果
- Rhyme/Java 设计模式之原型模式prototype通过clone方法和反序列化实现深复制
- C#匿名方法实现Java匿名类功能
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- JAVA实现图像缩放(通过 java.awt.geom的仿射变换结合java.awt.image的各种插值方法实现)
- C#用匿名方法定义委托的实现方法
- 在java中实现C#语法里的按引用传递参数的方法