写一个 ArrayList 的动态代理类,arraylist动态
分享于 点击 45975 次 点评:119
写一个 ArrayList 的动态代理类,arraylist动态
package TestProxy;
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 ProxyTest {
public static void main(String[] args) {
final ArrayList<String> list = new ArrayList<>();
List<String> listProxy=(List<String>) Proxy.newProxyInstance(list.getClass().getClassLoader(), list.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(list,args);
}
});
listProxy.add("你好") ;
System.out.println(list);
}
}
相关文章
- 暂无相关文章
用户点评