说明

1、JDK动态代理不需要实现界面,只有目标对象需要实现界面。

2、基于接口的动态代理需要使用JDK中的API,在JVM内存中动态构建Proxy对象。

3、需要使用java.lang.reflect.Proxy和newProxyInstance方法,但该方法需要接收三个参数。

实例

public class BusinessAgent implements Sell {
    private Vendor mVendor;
 
    public BusinessAgent(Vendor vendor) {
        this.mVendor = vendor;
    }
 
    public void sell() {
        System.out.println("before");
        mVendor.sell();
        System.out.println("after");
    }
 
    public void ad() {
        System.out.println("before");
        mVendor.ad();
        System.out.println("after");
    }
}
本文转载于:https://www.yisu.com/zixun/583692.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。