zoukankan      html  css  js  c++  java
  • 装饰器模式

    package shejimoshi.zhuangshiqi.car;
    //car接口
    public interface Car {
     public abstract void color();
     public abstract void size();
    }
    

      

    package shejimoshi.zhuangshiqi.car.juticar;
    
    import shejimoshi.zhuangshiqi.car.Car;
    //具体car(原版车)
    public class oldcar implements Car {
    
    	@Override
    	public void color() {
    		System.out.println(" ");
    
    	}
    
    	@Override
    	public void size() {
    	  System.out.println(" ");
    	}	
    
    }
    

      

    package shejimoshi.zhuangshiqi.car.zhuangshi;
    
    import shejimoshi.zhuangshiqi.car.Car;
    //装饰类
    public class zhuangshi implements Car {
    	protected Car car;
    
    	public zhuangshi(Car car) {
    		this.car = car;
    	}
    
    	public void color() {
    		car.color();
    
    	}
    	public void size() {
    		car.size();
    	}
    
    }
    

      

    package shejimoshi.zhuangshiqi.car.zhuangshi;
    
    import shejimoshi.zhuangshiqi.car.Car;
    //具体装饰车BMW(改造后)
    public class bmw extends zhuangshi {
    
    	public bmw(Car car) {
    		super(car);
    
    	}
    
    	public void color() {
    		car.color();
    		System.out.println("白色");
    	}
    
    	public void size() {
    		car.size();
    		System.out.println("大空间");
    	}
    }
    

      

    package shejimoshi.zhuangshiqi.car;
    
    import shejimoshi.zhuangshiqi.car.juticar.oldcar;
    import shejimoshi.zhuangshiqi.car.zhuangshi.bmw;
    import shejimoshi.zhuangshiqi.car.zhuangshi.zhuangshi;
    //测试类
    public class testcar {
     public static void main(String[] args) {
    	Car car=new oldcar();
    	car.color();
    	car.size();
    	zhuangshi BMW=new bmw(car);
    	BMW.color();
    	BMW.size();
    }
    }
    

      

  • 相关阅读:
    [POJ] 1979 Red and Black
    [Codeforces Round #192 (Div. 2)] D. Biridian Forest
    [Codeforces Round #192 (Div. 2)] B. Road Construction
    [Codeforces Round #192 (Div. 2)] A. Cakeminator
    430 vue组件命名方式: 短横线、驼峰
    429 vue脚手架
    428 webpack 使用步骤
    427 单页面应用,vue路由
    426 vue组件
    425 json-server,axios
  • 原文地址:https://www.cnblogs.com/ysg520/p/9593431.html
Copyright © 2011-2022 走看看