JDK动态代码:jdk动态代码是对接口的实现类进行增加,关键字接口,InvocationHandler(增强接口),Proxy代理类
//接口 第一步
public interface HelloInterface {
void sayHello();
}
//实现方法 第二步
public class Hello implements HelloInterface {
@Override
public void sayHello() {
System.out.println("Hello zhanghao!");
}
}
//处理程序 第三步
public class ProxyHandler implements InvocationHandler {
private Object object;
public ProxyHandler(Object object){
this.object=object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before invoice"+method.getName()); //调用方法前
method.invoke(object,args); //调用方法 功能增强
System.out.println("after invoke "+method.getName()); //调用方法后
return null;
}
}
public class test {
public static void main(String[] args) {
HelloInterface hell