
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) ;