package 适配器模式_对象适配;
public class 适配器模式_对象适配 {
public static void main(String[] args) {
VoltAdapter adapter = new VoltAdapter(new Volt220());
System.out.println("输出电压:"+adapter.getVolt5());
}
}
```java
package 适配器模式_对象适配;
public interface FiveVolt {
public int getVolt5();
}
package 适配器模式_对象适配;
public class Volt220 {
public int getVolt220() {
return 220;
}
}
package 适配器模式_对象适配;
public class VoltAdapter implements FiveVolt {
private Volt220 mVolt220;
public VoltAdapter (Volt220 volt220) {
mVolt220 = volt220;
}
@Override
public int getVolt5() {
return 5;
}
public int getVolt220() {
return mVolt220.getVolt220();
}
}