| 
 
| import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
 import java.util.List;
 
 public class Test23 {
 //写一个ArrayList类的代理,实现和ArrayList类中完全相同的功能,并可以计算每个方法运行的时间。
 
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 
 final ArrayList<?> tager = new ArrayList();
 List proxy =(List)Proxy.newProxyInstance(
 List.class.getClassLoader(),
 ArrayList.class.getInterfaces(),
 new InvocationHandler() {
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
 throws Throwable {
 long beginTime = System.currentTimeMillis();
 Thread.sleep(10);
 Object reVal = method.invoke(tager, args);
 long endTime = System.currentTimeMillis();
 System.out.println(method.getName()+"运行了"+(endTime-beginTime));
 return reVal;
 }
 });
 proxy.add("黑马程序员");
 proxy.add("你好");
 proxy.remove("你好");
 System.out.println(proxy.toString());
 }
 
 }
 
 | 
 |