Java设计模式-动态代理

#接口类名Isinger

public
interface Isinger { void Say(); }
#接口的实现类 Singer

public class Singer implements Isinger{
    @Override
    public void Say() {
        System.out.println("唱歌");
    }
}
#单元用例测试

public class TestDongProxy {
    @Test
    public void testDongProxy() {
        Singer s = new Singer();

//获取类的加载器,获取类的元素(方法 属性) Isinger isingerproxy
= (Isinger) Proxy.newProxyInstance(Singer.class.getClassLoader(), Singer.class.getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("我是巨星");//前置 Object o = method.invoke(s, args); System.out.println("唱的非常好!!!");//后置 return o; } }); isingerproxy.Say(); } }

 

posted @ 2021-08-02 10:56  BooleanQ  阅读(29)  评论(1)    收藏  举报