java 代理Proxy.newProxyInstance

来源:互联网 发布:女生low知乎 编辑:程序博客网 时间:2024/04/25 10:24
package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.swing.plaf.basic.BasicComboPopup.InvocationKeyHandler;

public class Test {
   public static void main(String[] args) {
       ShowMethod  showMethod =new TestValue();
    //   showMethod.getnae();
       InvocationHandler  interfacess=new TestPro(showMethod);
       
       
    ShowMethod  showMethod2=(ShowMethod) Proxy.newProxyInstance(showMethod.getClass().getClassLoader(),
            showMethod.getClass().getInterfaces(), interfacess);
    showMethod2.getnae();
    System.out.println(showMethod2.getClass().getName());
    System.out.println(showMethod.getClass().getName());
}
  static class TestPro implements InvocationHandler{
      Object  objects;
      public TestPro( Object  objects) {
          this.objects=objects;
        // TODO Auto-generated constructor stub
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("开始执行");
        // TODO Auto-generated method stub
        method.invoke(objects, args);
        System.out.println("结束执行");
        return null;
    }
       
   }
   interface ShowMethod {
       void getnae();
   }
  static class  TestValue implements ShowMethod{

    @Override
    public void getnae() {
        // TODO Auto-generated method stub
        System.out.println("执行");
    
    }
       
   }
}

0 0
原创粉丝点击