首页 > 代码库 > 装饰者简单的例子

装饰者简单的例子

package com.itheima.other;

import org.junit.Test;

public class WrapperTest {
	
	@Test
	public void fun01(){
		Qq qq = new Qq();
//		qq.run();
//		qq.stop();
		
		Car car = new WraperCar(qq);
		
		car.run();
		car.stop();
		
	}
	
	
	interface Car{
		void run();
		void stop();
		
	}
	
	class Qq implements Car{

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

		@Override
		public void stop() {
			System.out.println("刹车....");
		}
		
	}
	
	class WraperCar implements Car{
		
		Car car;
		
		
		public WraperCar(Car car) {
			this.car = car;
		}

		//改速度
		@Override
		public void run() {
			System.out.println("加了鸡血,跑100迈...");
			
		}

		//刹车不改
		@Override
		public void stop() {
			car.stop();
		}
		
	}

}

  

装饰者简单的例子