Java中
静态代理:
两个类实现同一个接口,真实角色对象通过代理角色的构造函数将参数传入,再执行代理角色的方法。
public class TestStaticProxy {
public static void main(String[] args) {
new WeddyCompany(new You()).happyMarry();
// new Thread(new Runnable对象).start();
}
}
//只能有一个public
interface Marry{
void happyMarry();
}
class You implements Marry{
@Override
public void happyMarry() {
System.out.println("终于结婚了");
}
}
class WeddyCompany implements Marry{
//真实角色
private Marrytarget;
WeddyCompany(Marry target){
this.target = target;
}
@Override
public void happyMarry() {
ready();
this.target.happyMarry();
after();
}
private void ready(){
System.out.println("收拾猪窝");
}
private void after(){
System.out.println("闹玉兔");
}
}