动态代理

`import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {

private Object target;

//设置目标角色(真实角色)
public void setTarget(Object target) {
    this.target = target;
}

//Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class[] { Foo.class },handler);
public Object getProxy(){
    return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}

//处理代理实例,返回结果
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return method.invoke(target,args);
}

}`

上一篇:SECS/GEM semiconductor communication development


下一篇:【Leetcode】1115. Print FooBar Alternately