天天看點

java jdk 代理

##main

public class Buyer  {
    public static void main(String[] args)  {
        taobao taobao = new taobao();
        SaleUDisk saleUDisk = (SaleUDisk) Proxy.newProxyInstance(taobao.getClass().getClassLoader(), taobao.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(taobao, 12);
                float ret = (Float) invoke + 11;
                return ret;
            }
        });
        float sale = saleUDisk.sale(10);
        System.out.println(sale);
    }
}      

##接口

public interface SaleUDisk {
    public float sale(Integer count);
}      
public class taobao implements SaleUDisk {
    private UDiskFactory uDiskFactory=new UDiskFactory();
    @Override
    public float sale(Integer count) {
        System.out.println("tao bao sale u disk 95");
        return 95f+count;
    }
}      
ide