代理类
public class ProxyInvocationHandler implements InvocationHandler {
private Rent rent;
public void setRent(Rent rent) {
this.rent = rent;
}
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(rent, args);
return result;
}
测试类
public class Client {
Host host=new Host();
ProxyInvocationHandler pih=new ProxyInvocationHandler();
pih.setRent(host);
Rent proxy=(Rent)pih.getProxy();
proxy.rent();
}