}
public class LiyuchunProxy {
private Person person ;
public Person createProxy(Person p){
this.person=p;
return (Person) Proxy.newProxyInstance(LiyuchunProxy.class.getClassLoader(), person.getClass().getInterfaces(), new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if(methodName.equals("sing")){
return method.invoke(person, args);
}else if(methodName.equals("dance")){
return method.invoke(person, args);
}else{
System.out.println("不支持");
return null;
}
}
});
}
}
public class ProxyTest {
public static void main(String[] args) {
Liyuchun cun = new Liyuchun();
LiyuchunProxy liyuchunProxy = new LiyuchunProxy();
Person person = liyuchunProxy.createProxy(cun);
String result = person.dance("机械");
System.out.println(result);
}
}