Home  >  Article  >  Java  >  Analyze the code of Java dynamic proxy instance

Analyze the code of Java dynamic proxy instance

WBOY
WBOYforward
2023-04-25 16:16:07846browse

Instructions

1. Use a proxy to wrap the object, and then use the proxy object to replace the original object.

2. Any call to the original object must go through the proxy. The proxy object determines if and when method calls are directed to the original object.

Example

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();
}
//输出结果
下单前,我先帮你看下有没有平台优惠券吧
好饿,今天午饭点外卖吧

The above is the detailed content of Analyze the code of Java dynamic proxy instance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete