适配器模式

public interface China220VInterface {
public void connect();
}
public class Chinae220VInterfaceImpl implements China220VInterface {
@Override
public void connect() {
System.out.println(“中国220v电源开始工作”);
}
}
public interface JP110VInterface {
public void connect();
}
public class JPInterfaceImpl implements JP110VInterface {
@Override
public void connect() {
System.out.println(“日本110v开始工作”);
}
}
public class ElectricCooker {
private JP110VInterface ap110VInterface;
ElectricCooker (JP110VInterface ap110VInterface){
this.ap110VInterface=ap110VInterface;
}
public void work(){
ap110VInterface.connect();;
System.out.print(“电饭煲开始工作”);

}

public void cooker(){
ap110VInterface.connect();
System.out.println(“电饭煲开始煮蛋”);
}

}
public class PowerAdapter implements JP110VInterface {
private China220VInterface china220VInterface;
PowerAdapter(China220VInterface china220VInterface){
this.china220VInterface=china220VInterface;
}

@Override
public void connect() {
  china220VInterface.connect();
}

}
public class AdapterTest {
public static void main(String[] args) {
China220VInterface china220VInterface=new Chinae220VInterfaceImpl();
PowerAdapter powerAdapter=new PowerAdapter(china220VInterface);
ElectricCooker electricCooker=new ElectricCooker(powerAdapter);
electricCooker.work();
electricCooker.cooker();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值