- SubJect.class
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-10
- * Time: 15:07:40
- * To change this template use File | Settings | File Templates.
- */
- public interface SubJect {
- public void info();
- }
- RealSubJect.class
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-10
- * Time: 15:08:33
- * To change this template use File | Settings | File Templates.
- */
- public class RealSubJect implements SubJect{
- @Override
- public void info(){
- System.out.println("这是真实的自我");
- }
- }
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-10
- * Time: 15:11:26
- * To change this template use File | Settings | File Templates.
- */
- public class ProxySubJect implements InvocationHandler {
- private Object inObj;
- public ProxySubJect(Object obj){
- this.inObj=obj;
- }
- @Override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- // TODO Auto-generated method stub
- method.invoke(inObj, args);
- return null;
- }
- }
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-11-10
- * Time: 15:15:35
- * To change this template use File | Settings | File Templates.
- */
- public class Test {
- public static void main(String[] args){
- RealSubJect real=new RealSubJect();
- ProxySubJect proxy = new ProxySubJect(real);
- SubJect subject=(SubJect)Proxy.newProxyInstance(proxy.getClass().getClassLoader(),real.getClass().getInterfaces(),proxy) ;
- subject.info();
- }
- }
转载于:https://blog.51cto.com/532770034/711077