reference:http://m.oschina.net/blog/224931
中文版 page:https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/proxy.html
1 Public Interfacecolorable {2 3 Public voidvalue ();4 5 }6 7 Public classRedcolorImplementscolorable {8 9 @OverrideTen Public voidvalue () { OneSystem.out.println ("--------------Red-------------"); A } - - } the - Public classColorableproxyImplementsInvocationhandler { - - Privatecolorable colorable; + Privatecolorable proxy; - + Publiccolorableproxy (colorable colorable) { A This. colorable =colorable; at This. Proxy = (colorable) proxy.newproxyinstance (colorable.class. getClassLoader (),NewClass<?>[] {colorable.class}, This); - } - - Publiccolorable GetProxy () { - returnproxy; - } in - @Override to Publicobject Invoke (Object proxy, Method method, object[] args) + throwsThrowable { -String MethodName =method.getname (); the *System.out.println ("===========starting invoke function:" + methodName + "=========="); $ Panax NotoginsengObject result =Method.invoke (colorable, args); - theSystem.out.println ("=========== invoke function:" + methodName + "success=========="); + returnresult; A } the + } - $ Public classMain { $ - Public Static voidMain (string[] args) { - theColorable Proxy =NewColorableproxy (NewRedcolor ()). GetProxy (); - Proxy.value ();Wuyi } the -}
On the Proxy+invocationhandler of Java