public class ProxyTest
{
   public static void main(String[] args)
   {
      Object[] elements = new Object[1000];      // fill elements with proxies for the integers 1 ... 1000
      for (int i = 0; i < elements.length; i++)
      {
         Integer value = i + 1;
         InvocationHandler handler = new TraceHandler(value);
         Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class } , handler);
         elements[i] = proxy;
      }      // construct a random integer
      Integer key = new Random().nextInt(elements.length) + 1;      // search for the key
      int result = Arrays.binarySearch(elements, key);      // print match if found
      if (result >= 0) System.out.println(elements[result]);
   }
}
这是代码的一部分,代码不重要,我只想问问其中    Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class } , handler);这句代码怎么理解,尤其是Comparable.class这代表什么,这样写是什么意思~~~

解决方案 »

  1.   

    3个参数第一个是类加载器,第二个是接口,第三个是invocationhandle 。Comparable.class代表拿到一个接口的class对象。jdk的动态代理必须要有接口,否则实现步了
      

  2.   

    我也正在看这本书,呵呵!
    动态代理,是有点儿小麻烦的。看一下它的API:这个参数就是the list of interfaces for the proxy class to implement。慢慢理解呗!