例子:通过一个代理类(Person.java)动态调用两个相似类(Man.java、Woman.java)。其中,代理类用java反射机制实现。参照:http://java.chinaitlab.com/advance/805648.html
Man.java
public class Man { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void introduce(String name,Integer age){ System.out.println("name="+name+"& age="+age); } }
Woman.java
public class Woman { public Woman(){ } public Woman(String name,int age){ this.name = name; this.age = age; } private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void introduce(){ System.out.println("name:"+name+" &age:"+age); } }
代理类Person.java
import java.lang.reflect.*; public class Person { public Person(){ } private Object target; // 目标对象 private String methodName; // 目标方法 private Object[] params; // 参数数组 private Method method; public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public Object[] getParams() { return params; } public void setParams(Object[] params) { this.params = params; } public Method getMethod() { return method; } public void setMethod(Method method) { this.method = method; } /* * 重新设置目标方法 */ public void reSetMethod(String methodName) throws ClassNotFoundException{ if(params!=null){ int paramsLength = params.length; Class<?>[] paramsTypes = new Class<?>[paramsLength]; for(int i=0;i<paramsLength;i++){ paramsTypes[i] = params[i].getClass(); }//end for try{ method = target.getClass().getMethod(methodName, paramsTypes); }catch(Exception e){ e.printStackTrace(); } }else{ try{ method = target.getClass().getMethod(methodName, null); }catch(Exception e){ e.printStackTrace(); } } }//end method /** * 重新设置目标对象和方法 * @throws ClassNotFoundException */ public void reSetTarget(Object target,String methodName) throws ClassNotFoundException{ this.target = target; this.reSetMethod(methodName); } /** * 动态调用已绑定的方法 */ public void doMethod() throws Exception{ reSetTarget(target,methodName); this.method.invoke(target, params); } }
调用代理类
import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class ProxyTest { public void invoke(String className,Class<?>[] calsses,Object[] constructsParams,String methodName,Object[] params)throws Exception{ Class<?> clazz = Class.forName(className); Constructor<?> cons = clazz.getConstructor(calsses); Object obj = cons.newInstance(constructsParams); Person person = new Person(); person.setTarget(obj); person.setMethodName(methodName);//映射方法名 person.setParams(params);//映射方法参数 person.doMethod();//通过反射调用方法 } /** * @throws Exception * @功能描述 * @输入参数 * @反馈值 */ public static void main(String[] args) throws Exception { /** * 运行时动态调用相似类 */ ProxyTest test = new ProxyTest(); test.invoke("com.sinye.reflect.Man", null, null, "introduce", new Object[]{new String("梁山伯"),25}); test.invoke("com.sinye.reflect.Woman",new Class<?>[]{String.class,int.class}, new Object[]{"祝英台",new Integer(22)}, "introduce", null); /** * 运行时调用Man类的setName和getName方法 */ Class cc = Class.forName("com.sinye.reflect.Man"); Object object = cc.newInstance(); Method m = cc.getDeclaredMethod("setName", new Class[]{String.class}); m.invoke(object, new Object[]{new String("shanbo")}); m = cc.getDeclaredMethod("getName", null); String name = (String) m.invoke(object, null); System.out.println(name); } }