通过反射创建动态代理对象(二)
生活随笔
收集整理的這篇文章主要介紹了
通过反射创建动态代理对象(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
一、概述
? ? 將“通過反射創建動態代理對象(一)”合二為一
二、代碼說明
package staticimport.proxy;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.Collection;/**** * 將ProxyTest.java兩步操作合二為一* * @author Liu**/ public class Proxy2Integration {public static void main(String[] args) {Collection collection = (Collection) Proxy.newProxyInstance(Collection.class.getClassLoader(), new Class[]{Collection.class}, new InvocationHandler() {Collection<String> collection = new ArrayList<>();//這里打印值才為3@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Collection<String> collection = new ArrayList<>();//這里打印值為0//死循環 // return method.invoke(proxy, args);long startTime = System.currentTimeMillis();Object object = method.invoke(collection, args);long endTime = System.currentTimeMillis();System.out.println("Time consume: " + (endTime - startTime) + " ms");return object;}});collection.add("a");collection.add("b");collection.add("c");System.out.println(collection.size());//除了Object累的hashCode()/equals()/toString()方法會通過代理調用,其它所有方法都是直接調用自身System.out.println(collection.getClass().getName());}}?
轉載于:https://my.oschina.net/Howard2016/blog/1617989
總結
以上是生活随笔為你收集整理的通过反射创建动态代理对象(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运用node实现简单爬虫
- 下一篇: CentOS6.9中搭建FTP服务器