public void single();
}
public class Singer implements ISinger {
@Override
public void single() {
System.out.println("唱一首歌");
}
}
public class DynamicProxy implements InvocationHandler {
private Object single;
public DynamicProxy(Object single) {
this.single = single;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("向观众问好");
method.invoke(single, args);
System.out.println("谢谢大家");
return null;
}
}
public class Test {
public static void main(String[] args) {
ISinger singer = new Singer();
InvocationHandler hander = new DynamicProxy(singer);
ISinger iSinger = (ISinger) Proxy.newProxyInstance(hander.getClass().getClassLoader(), singer.getClass().getInterfaces(),
hander);
iSinger.single();}
}