//1.定义目标被适配类
//2.定义适配器接口,期望输出
//3.实现适配器接口,被适配对象,通过set/构造方法注入进行组合
//4.测试
/**
- 被适配类,输出的是220V
*/
public class Voltage220V {
public int outPut220V(){
return 220;
}
}
/**
- 提供适配接口
/
public interface IVoltage5V {
/*- 适配接口
- @return
*/
int output5V();
}
/**
-
实现适配器接口,被适配对象,通过set/构造方法注入进行组合
*/
public class VoltageAdapter implements IVoltage5V {private Voltage220V voltage220V;
public VoltageAdapter(Voltage220V voltage220V) {
this.voltage220V = voltage220V;
}public int output5V() {
int src = voltage220V.outPut220V();
return src/44;
}
}
public class AdapterTest {
public static void main(String[] args) {
VoltageAdapter voltageAdapter=new VoltageAdapter(new Voltage220V());
System.out.println(voltageAdapter.output5V());
}
}