package com.fc.Proxy; public interface KindWoman { public void makeEyesWithMan(); //抛媚眼 }
package com.fc.Proxy; public class PanJinlian implements KindWoman { public void makeEyesWithMan() { System.out.println("潘金莲在抛媚眼。。。"); } }
再来个王婆,也就是潘金莲的代理人:
package com.fc.Proxy; public class WangPo implements KindWoman { private KindWoman kindwoman; public WangPo() { this. kindwoman = new PanJinlian();//王婆默认是潘金莲的代理人 } //王婆也可以是KindWomen的其他任何一个女人的代理,只要你是这一类型 public WangPo(KindWoman kindWomen){ this. kindwoman = kindWomen; } public void makeEyesWithMan() { this. kindwoman.makeEyesWithMan(); //王婆这么大年龄了,谁看她抛媚眼?!她代替别人抛 } }连个女猪脚已经登场,下面到西门庆了:
package com.fc.Proxy; public class XiMenQing { public static void main(String[] args) { //把王婆叫出来 WangPo wangPo = new WangPo(); //然后西门庆就说,我要和潘金莲happy,然后王婆就安排了西门庆丢筷子的那出戏: wangPo.makeEyesWithMan(); //看到没,虽然表面上时王婆在做,实际上爽的是潘金莲 } }
package com.fc.Proxy; public class PanJinlian222 implements KindWoman { public void makeEyesWithMan() { System.out.println("潘金莲222在抛媚眼。。。"); } }
package com.fc.Proxy; public class XiMenQing { public static void main(String[] args) { /* //把王婆叫出来 WangPo wangPo = new WangPo(); //然后西门庆就说,我要和潘金莲happy,然后王婆就安排了西门庆丢筷子的那出戏: wangPo.makeEyesWithMan(); //看到没,虽然表面上时王婆在做,实际上爽的是潘金莲 */ WangPo wangPo=new WangPo(new PanJinlian222());//这个不是默认代理,要往里传个参数 wangPo.makeEyesWithMan(); } }
原文地址:http://blog.csdn.net/fangchao3652/article/details/43266865