JAVA 反射 动态代理
作者:互联网
interface HelloInterface { String sayHello(); } class Hello implements HelloInterface{ @Override public String sayHello() { System.out.println("Hello zhanghao!"); return "Hello"; } } class ProxyHandler implements InvocationHandler { private Object object; public ProxyHandler(Object object){ this.object = object; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before invoke " + method.getName()); Object value = method.invoke(object, args); System.out.println("After invoke " + method.getName()); return value; } } public class proxyTest { public static void main(String[] args) { HelloInterface hello = new Hello(); InvocationHandler handler = new ProxyHandler(hello); HelloInterface proxyHello = (HelloInterface) Proxy.newProxyInstance(hello.getClass().getClassLoader(), hello.getClass().getInterfaces(), handler); Object value = proxyHello.sayHello(); System.out.println(value); }
标签:反射,object,JAVA,Object,代理,System,HelloInterface,public,out 来源: https://www.cnblogs.com/superxuezhazha/p/12358437.html