zoukankan      html  css  js  c++  java
  • 动态代理

     1 package DynamicProxy;
     2 
     3 import java.lang.reflect.InvocationHandler;
     4 import java.lang.reflect.Method;
     5 import java.lang.reflect.Proxy;
     6 
     7 interface AbstractClass {
     8     public void show();
     9 }
    10 
    11 class ClassA implements AbstractClass {
    12     public void show() {
    13         System.out.println("I am A !");
    14     }
    15 }
    16 
    17 class ClassB implements AbstractClass {
    18     public void show() {
    19         System.out.println("I am B !");
    20     }
    21 }
    22 
    23 class Invoker implements InvocationHandler {
    24     AbstractClass ac;
    25     public Invoker(AbstractClass ac) {
    26         this.ac = ac;
    27     }
    28     
    29     @Override
    30     public Object invoke(Object proxy, Method method, Object[] args)
    31             throws Throwable {
    32         method.invoke(ac, args);
    33         return null;
    34     }
    35     
    36 }
    37 
    38 public class DynamicProxyTest {
    39     public static void main(String[] args) {
    40         
    41         Invoker invoker1 = new Invoker(new ClassA());
    42         AbstractClass ac1 = (AbstractClass) Proxy.newProxyInstance(AbstractClass.class.getClassLoader(), new Class[] {AbstractClass.class}, invoker1);
    43         ac1.show();
    44         
    45         Invoker invoker2 = new Invoker(new ClassB());
    46         AbstractClass ac2 = (AbstractClass) Proxy.newProxyInstance(AbstractClass.class.getClassLoader(), new Class[] {AbstractClass.class}, invoker2);
    47         ac2.show();
    48     }
    49 }
    我喜欢一无所有,这样就只能一步一步的创造世界...
  • 相关阅读:
    【转】运行维护管理制度
    系统负载超预警 问题定位
    19-多进程学习
    3-Pandas层次化索引&拼接
    2-Anaconda简介&Numpy基础
    1-IPython&jupyter notebook
    18-进程&协程
    17-多线程
    16-网络通信
    15-正则表达式
  • 原文地址:https://www.cnblogs.com/riordon/p/4018337.html
Copyright © 2011-2022 走看看