装饰者模式

2019-05-07  本文已影响0人  hgzzz
public interface Car {
    void start();
    void run();
    void stop();
}
public class MyCar implements Car{

    @Override
    public void start() {
        System.out.println("start");
    }

    @Override
    public void run() {
        System.out.println("run");
    }

    @Override
    public void stop() {
        System.out.println("stop");
    }
}
public class MyCarPlus implements Car {

    private Car car;

    public MyCarPlus( Car car){
        this.car = car;
    }
    @Override
    public void start() {
        System.out.println("before start,play some music ");
        car.start();
    }

    @Override
    public void run() {
        car.run();

    }

    @Override
    public void stop() {
        car.stop();
    }
}
public class Run {
    public static void main(String[] args) {
        MyCarPlus myCarPlus = new MyCarPlus(new MyCar());
        myCarPlus.start();
    }
}
上一篇下一篇

猜你喜欢

热点阅读