您现在的位置是:课程教程文章

java动态代理的原理

2023-12-14 20:51课程教程文章 人已围观

说明

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学习指路:Java基础

本教程操作环境:windows7系统、java10版,DELL G3电脑。

课程教程:java动态代理的原理

上一篇:使用爬虫代理ip的注意事项

下一篇:没有了

站点信息

  • 文章统计篇文章