import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxy {
public Object getProxy(final Object obj) {
return Proxy.newProxyInstance(DynamicProxy.class.getClassLoader(),
obj.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(obj, args);
}
});
}
}
class Test {
public static void main(String[] args){
DynamicProxy dp = new DynamicProxy();
Animal(接口) bird(接口实例) = (接口)dp.getProxy(new Bird()(实现接口类的实例));
bird.fly();
}
}
接口与实现类省略...