Skip to content

Instantly share code, notes, and snippets.

@wendal
Created February 6, 2012 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wendal/1749430 to your computer and use it in GitHub Desktop.
Save wendal/1749430 to your computer and use it in GitHub Desktop.
演示简单Proxy代理类
public static void main(String[] args) {
List list = (List) Proxy.newProxyInstance(List.class.getClassLoader(),
new Class[] { List.class },
new InvocationHandler() {
public Object invoke(Object obj, Method method, Object[] args)
throws Throwable {
if ("size".equals(method.getName()))
return (int)(Math.random() * 1000);
return null;
}
});
for (int i = 0; i < 10; i++) {
System.out.println(list.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment