首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

反照 静态代理 动态代理

2012-12-19 
反射 静态代理 动态代理import java.lang.reflect.InvocationHandler import java.lang.reflect.Proxy i

反射 静态代理 动态代理

import java.lang.reflect.InvocationHandler ;

import java.lang.reflect.Proxy ;

import java.lang.reflect.Method ;

interface Subject{

public String say(String name,int age) ;// 定义抽象方法say

}

class RealSubject implements Subject{// 实现接口

public String say(String name,int age){

return "姓名:" + name + ",年龄:" + age ;

}

};

class MyInvocationHandler implements InvocationHandler{

private Object obj ;

public Object bind(Object obj){

this.obj = obj ;// 真实主题类

return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this) ;

}

public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{

Object temp = method.invoke(this.obj,args) ;// 调用方法

return temp ;

}

};

public class DynaProxyDemo{

public static void main(String args[]){

Subject sub = (Subject)new MyInvocationHandler().bind(new RealSubject()) ;

String info = sub.say("李兴华",30) ;

System.out.println(info) ;

}

};

?

?

?

?

?

热点排行