public interface IStudy_service {
void study(String s);
void sing(String song);
String whoAreU();
}
public class Student implements IStudy_service {
@Override
public void study(String s) {
}
@Override
public void sing(String song) {
}
@Override
public String whoAreU() {
return "张三";
}
}
public class DongTaiDaiLi {
public static void main(String[] args) {
//代理Student对象
IStudy_service mProxy = test_interface_proxy(new Student());
mProxy.study("学语文");
mProxy.sing("我爱我的祖国");
System.out.println("他的名字是" + mProxy.whoAreU());
}
public static IStudy_service test_interface_proxy(Student student) {
return (IStudy_service) Proxy.newProxyInstance(IStudy_service.class.getClassLoader(),
new Class<?>[]{IStudy_service.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("method" + method);
//System.out.println("args[0]" + args[0]);
if ("study".equals(method.getName())) {
System.out.println("study");
} else if ("sing".equals(method.getName())) {
System.out.println("sing");
} else if ("whoAreU".equals(method.getName())) {
//替换学生的名字
String result = (String) method.invoke(student, null);
System.out.println("这个学生真正的名字是" + result);
return "李四";
}
//method.invoke(proxy,args);
return null;
}
}
);
}
}
输出
methodpublic abstract void com.iffy.mianshi.pattern.proxy.dongtaidaili.IStudy_service.study(java.lang.String)
study
methodpublic abstract void com.iffy.mianshi.pattern.proxy.dongtaidaili.IStudy_service.sing(java.lang.String)
sing
methodpublic abstract java.lang.String com.iffy.mianshi.pattern.proxy.dongtaidaili.IStudy_service.whoAreU()
这个学生真正的名字是张三
他的名字是李四