代理在生活比较常见, 类似于代购。
public static void main(String[] args) {
BuyOnOther buyOnOther = (BuyOnOther) Proxy.newProxyInstance(BuyOnOther.class.getClassLoader(), new Class<?>[]{BuyOnOther.class}, (proxy, method, args1) -> {
String name = (String) args1[0];
Integer count = (Integer) args1[1];
System.out.println("参数: " + name + "," + count);
System.out.println("方法名: " + method.getName());
return null;
});
buyOnOther.buy("面膜", 5);
}
interface BuyOnOther {
void buy(String name, int count);
}
输出结果:
参数: 面膜,5
方法名: buy
网友评论