适配器模式

概述

 

 

代码示例

方法1(继承的方式)

public class Current {
	
	public void use220V() {
		System.out.println("使用220V电流");
	}
}
public class Adapter extends Current{
	public void use18V() {
		System.out.println("使用适配器");
		this.use220V();
	}
}

主函数

public class MainClass {
	public static void main(String[] args) {
//		Current current = new Current();
//		current.use220V();
		
//		Adapter adapter = new Adapter();
//		adapter.use18V();
		
		Adapter2 adapter = new Adapter2(new Current());
		adapter.use18V();
	}
}

方法2

public class Adapter2 {
	private Current current;
	
	public Adapter2(Current current) {
		this.current = current;
	}
	
	public void use18V() {
		System.out.println("使用适配器");
		this.current.use220V();
	}
}