代理模式:
1.真实角色
2.代理角色:持有真实角色的引用
3,两者实现相同的接口
package test; public class Java静态代理 { public static void main(String[] args) { // TODO Auto-generated method stub Marry you =new You(); WeddingCompany company=new WeddingCompany(you); company.marry(); } } //接口 interface Marry{ public abstract void marry();//前面若没有修饰符默认为public abstract } //真实角色 class You implements Marry{ @Override public void marry() { // TODO Auto-generated method stub System.out.println("you和某某某结婚"); } } //代理角色 class WeddingCompany implements Marry{ //需要持有真实角色的引用 private Marry you; public WeddingCompany() { super(); }//无参构造函数 public WeddingCompany(Marry you) { super(); this.you = you; }//带参数的构造函数 public void before(){ System.out.println("布置新房"); } @Override public void marry() { // TODO Auto-generated method stub before(); you.marry(); after(); } public void after(){ System.out.println("闹伴娘"); } }