说明
1、使用代理将对象包装起来,然后用该代理对象取代原始对象。
2、任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。
实例
public static void main(String[] args) { Roommate roommate = new Roommate("zhangsan"); Hungry proxyInstance = (Hungry) Proxy.newProxyInstance( roommate.getClass().getClassLoader(), roommate.getClass().getInterfaces(), new RoommateInvocationHandler<Roommate>(roommate) ); proxyInstance.callLunch(); } //输出结果 下单前,我先帮你看下有没有平台优惠券吧 好饿,今天午饭点外卖吧
以上就是java动态代理的原理,希望对大家有所帮助。更多Java学习指路:
本教程操作环境:windows7系统、java10版,DELL G3电脑。
Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务