C#下面的Delegate可以让方法传递方法,在Java下却没有类似的机制。如何在Java下面模拟出Delegate呢?使用反射试试。
贴代码:
/** author:licunqing */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Delegate { private Class<?> c=null; private String method=null; private Class<?>[] paramsType=null; private Object[] params; public Delegate(Class<?> c,String method,Class<?>[] paramsType,Object[] params) { this.c=c; this.method=method; this.paramsType=paramsType; this.params=params; } public void invoke() throws NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method m = c.getDeclaredMethod(method, paramsType); Object o = c.newInstance(); m.invoke(o, params); } }