JDK的动态实现代码-->Proxy.newProxyInstance()

来源:互联网 发布:mac弹出u盘 编辑:程序博客网 时间:2024/04/30 11:02
interface ProxyInterFace{ public void proxyMethod();}class TargetObject implements ProxyInterFace{ public void proxyMethod() {  System.out.println("我被代理了,哈哈!"); }}class ProxyObject implements InvocationHandler{ //代码的对象 public Object targetObject;  public void setTargetObject(Object targetObject) {  this.targetObject = targetObject; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  //调用,传入一个目标对象,和对应的对象参数  return method.invoke(targetObject, args); }}public class ProxyMain{ public static void main(String[] args) {  //代理的目标对象  ProxyInterFace proxyInterface = new TargetObject();  //代理器  ProxyObject proxyObject = new ProxyObject();  proxyObject.setTargetObject(proxyInterface);  //转换成InvocationHandler  InvocationHandler handler = proxyObject;  //执行代码任务  Object proxy =  Proxy.newProxyInstance(proxyInterface.getClass().getClassLoader(), proxyInterface.getClass().getInterfaces(),handler );  //转换成目标对象,调用目标对象的方法  ((ProxyInterFace)proxy).proxyMethod(); }}