说明

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)
        );
        proxyInstance.callLunch();
}
//输出结果
下单前,我先帮你看下有没有平台优惠券吧
好饿,今天午饭点外卖吧
本文转载于:https://www.yisu.com/zixun/583436.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。