java¶¯Ì¬´úÀí
¶¯Ì¬´úÀíÊÇÖ¸¿Í»§Í¨¹ý´úÀíÀàÀ´µ÷ÓÃÆäËü¶ÔÏóµÄ·½·¨
¶¯Ì¬´úÀíʹÓó¡ºÏ:
•
Ô¶³Ì·½·¨µ÷ÓÃ(RMI)
•
1.´´½¨Ò»¸öʵÏÖ½Ó¿ÚInvocationHandlerµÄÀ࣬Ëü±ØÐëʵÏÖinvoke·½·¨
2.´´½¨±»´úÀíµÄÀàÒÔ¼°½Ó¿Ú
3.ͨ¹ýProxyµÄ¾²Ì¬·½·¨
newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h) ´´½¨Ò»¸ö´úÀí
4.ͨ¹ý´úÀíµ÷Ó÷½·¨
²Î¼û³ÌÐòVectorProxy.java
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;
public class VectorProxy implements InvocationHandler
{
private Object proxyobj;
public VectorProxy(Object obj)
{
proxyobj = obj;
}
public static Object factory(Object obj)
{
Class<?> cls = obj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(),
cls.getInterfaces(), new VectorProxy(obj));
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
System.out.println("before calling " + method);
if (args != null)
{
for (int i = 0; i < args.length; i++)
{
System.out.println(args[i] + "");
}
}
Object object = method.invoke(proxyobj, args);
System.out.println("after calling " + method);
return object;
}
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
List<String> v = (List<String>) factory(new Vector<String>(10));
v.add("New");
v.add("York");
System.out.println(v);
v.remove(0);
System.out.println(v);
}
}
²Î¼û³ÌÐòFoo.java
²Î¼û³ÌÐòFooImpl.java
²Î¼û³ÌÐòFooImpl2.java
²Î¼û³ÌÐòCommonInvocationHandler.java
²Î¼û³ÌÐòDemo.java
package com.langsin.dynamicproxy;
public interface Foo
{
void doAction();
}
package com.langsin.dynamicproxy;
public class FooImpl implements Foo
{
public FooImpl()
{
}
public void doAction()
{
System.out.println("in FooImp1.doActio
Ïà¹ØÎĵµ£º
LombokÊÇÒ»¸ö¿ªÔ´ÏîÄ¿£¬¿ÉÒÔÓÃÀ´Ïû³ýJava´úÂëµÄÈß³¤
Ïêϸ½éÉܲο´http://www.ibm.com/developerworks/opensource/library/os-lombok/index.html
ÏÂÃæ½éÉÜLombokÈçºÎÏû³ýJava´úÂëµÄÈß³¤¡£
1. Ê×ÏÈÈ¥http://projectlombok.org/. ÏÂÔØlombok.jar °ü
2. & ......
¹¹ÔìÆ÷ÊÇΪÁË´´½¨Ò»¸öÀàµÄʵÀý¡£Õâ¸ö¹ý³ÌÒ²¿ÉÒÔÔÚ´´½¨Ò»¸ö¶ÔÏóµÄʱºòÓõ½£ºPlatypus p1 = new Platypus();
¡¡¡¡Ïà·´£¬·½·¨µÄ×÷ÓÃÊÇΪÁËÖ´ÐÐjava´úÂë¡£
¡¡¡¡ÐÞÊηû£¬·µ»ØÖµºÍÃüÃûµÄ²»Í¬
¡¡¡¡¹¹ÔìÆ÷ºÍ·½·¨ÔÚÏÂÃæÈý¸ö·½±ãµÄÇø±ð£ºÐÞÊηû£¬·µ»ØÖµ£¬ÃüÃû¡£ºÍ·½·¨Ò»Ñù£¬¹¹ÔìÆ÷¿ÉÒÔÓÐÈκηÃÎʵÄÐÞÊΣº public, protected ......
¼øÓÚÐí¶àͬѧ¶ÔÕâ¸ömain·½·¨µÄÃÔ»ó£¡ÎÒÕâÀïÌṩһЩÒâ¼û£¬Ï£Íû¶Ô´ó¼ÒÓаïÖú£¡ºÇºÇ£¡
****ÎÒÃÇ˵¼ÓÔØÏÈ£º
JavaµÄÀàÊÇÓõ½²Å¿ªÊ¼¼ÓÔصģ¬²¢ÇÒÔÚÿ¸öÀàµÄ¹¹ÔìÆ÷µ÷ÓÃ֮ǰ£¬ÀàµÄÿ¸öÓò£¨³ÉÔ±±äÁ¿£©¶¼ÒªÊ×Ïȱ»±àÒëÆ÷³õʼ»¯Ò»´Î£¬È»ºó²ÅÂÖµ½¹¹ÔìÆ÷À´¸É»î¶ù¡£µ«ÊÇ£¬Java ......
public class chengfa {
public static void main(String[] args)
{
model md=new model();
md.method();
}
}
class model
{
public int count=0;
public int[]b=new int[100];
public void me ......
ͨ³££¬¿Í»§Àà(clients of class)ͨ¹ýÀàµÄ½Ó¿Ú·ÃÎÊËüÌṩµÄ·þÎñ¡£ÓÐʱ£¬ÏÖÓеÄÀà(existing class)¿ÉÒÔÌṩ¿Í»§ÀàµÄ¹¦ÄÜÐèÒª£¬µ«ÊÇËüËùÌṩµÄ½Ó¿Ú²»Ò»¶¨ÊÇ¿Í»§ÀàËùÆÚÍûµÄ¡£ÕâÊÇÓÉÓÚÏÖÓеĽӿÚÌ«Ïêϸ»òÕßȱ·¦Ïêϸ»ò½Ó¿ÚµÄÃû³ÆÓë¿Í»§ÀàËù²éÕҵIJ»Í¬µÈÖî¶à²»Í¬ÔÒòµ¼Öµġ£
¡¡¡¡ÔÚÕâÖÖÇé¿öÏ£¬ÏÖÓеĽӿÚÐèҪת»¯(convert)Ϊ¿ ......