标签:get oca public voc owa 联想 stat tcl reflect
package com.fgy.proxy; public interface SaleComputer { String sale(Double money); void show(Double money); }
package com.fgy.proxy; public class Lenovo implements SaleComputer { @Override public String sale(Double money) { System.out.println("联想电脑卖" + money + "元。"); return "联想电脑"; } @Override public void show(Double money) { System.out.println("联想电脑,价格:" + money); } }
package com.fgy.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * 动态代理测试 */ public class ProxyTest { public static void main(String[] args) { // 1.创建真实对象 Lenovo lenovo = new Lenovo(); // 2.动态代理增强lenovo SaleComputer proxy_lenovo = (SaleComputer) Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("sale")) { System.out.println("专车接送:"); double money = (double) args[0] * 1.25; String obj = (String) method.invoke(lenovo, money); System.out.println("免费送货。"); return obj + "_送鼠标垫"; } else { Object obj = method.invoke(lenovo, args); return obj; } } }); // 3.调用方法 String computer = proxy_lenovo.sale(10000.99); System.out.println(computer); // proxy_lenovo.show(8000.99); } }
标签:get oca public voc owa 联想 stat tcl reflect
原文地址:https://www.cnblogs.com/roadlandscape/p/12241411.html