A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐启坤 中级黑马   /  2013-6-8 20:18  /  1384 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 徐启坤 于 2013-6-8 21:15 编辑

张老师的这段代码中有两点不明白,见代码

private static Object getProxy(final Object target,final Advice advice) {                //为什么这里的参数要用final修饰?
        Object proxy3 = Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                /*new Class[]{Collection.class},*/                           //这里应该是要接受数组形式的参数
                target.getClass().getInterfaces(),                            //为什么这里却没用数组??
                new InvocationHandler(){
               
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {

                        /*long beginTime = System.currentTimeMillis();
                        Object retVal = method.invoke(target, args);
                         long endTime = System.currentTimeMillis();
                        System.out.println(method.getName() + " running time of " + (endTime - beginTime));
                        return retVal;*/
                        

                        advice.beforeMethod(method);
                        Object retVal = method.invoke(target, args);
                        advice.afterMethod(method);
                        return retVal;                        
                        
                    }
                }
                );
        return proxy3;
    }


评分

参与人数 1技术分 +1 收起 理由
黑马伍哲沂 + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
加final是因为内部类访问外部局部变量。

另外,参数本来就是数组。因为getInterfaces()返回的是一个数组。
回复 使用道具 举报
本帖最后由 徐启坤 于 2013-6-8 21:15 编辑
黑马伍哲沂 发表于 2013-6-8 21:03
加final是因为内部类访问外部局部变量。

另外,参数本来就是数组。因为getInterfaces()返回的是一个数组。 ...

哦看来好几遍都没有发现,原来是用了getInterfaces(),我还以为只返回一个接口来
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马