import java.lang.reflect.*;
public class TestAgent {
public static void main(String[] args) {
Agent agent=new Agent();
BAgentFace bAgentFace=null;
agent.setObj(new BAgent());
bAgentFace=(BAgentFace)Proxy.newProxyInstance(BAgentFace.class.getClassLoader(),new Class[]{BAgentFace.class},agent);
bAgentFace.add();
}
}
class Agent implements InvocationHandler
{
private Object obj;
public void setObj(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(obj,args);
}
}
class BAgent implements BAgentFace
{
public void add()
{
System.out.println("ni hao");
}
}
interface BAgentFace
{
public void add();
}
这个是具体的例子
他应用在junit可以测试 某一个方法 |