首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

装饰器模式

2024-12-12 来源:要发发知识网
public interface Car {

    void move();

}
public class DecoratorCar implements Car {

    private Car c;

    public void move(){
        c.move();
    }

    DecoratorCar(Car c) {
        this.c = c;
    }
}

public class FlyCar extends DecoratorCar {

    FlyCar(Car c) {
        super(c);
    }

    public void move() {
        super.move();
        System.out.println("fly");
    }
}

public class RealCar implements Car  {

    public void move(){
        System.out.println("real");
    }
}
显示全文